⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 testcf.c

📁 这是一个基于AVR单片机的CF卡访问函数集和一个应用例子
💻 C
字号:
#include <avr\io.h>
#include <avr\interrupt.h>
#include <avr\signal.h>
#include <stdio.h>
#include <stdlib.h>
#include "CF_IO.h"

long timer = 0;	//保存时间的变量,单位1ms

SIGNAL(SIG_OVERFLOW0)	//计时器0中断处理程序
{
	TCNT0 = 0x83;	//1ms at 8MHZ
	cbi(TIFR, 7);
	timer ++;	//timer加1ms
}

int usart_putchar(char c)
{
	if(c == '\n')
	usart_putchar('\r');
	loop_until_bit_is_set(UCSRA, UDRE);
	UDR = c;
	return 0;
}

int usart_getchar(void)
{
	loop_until_bit_is_set(UCSRA, RXC);
	return UDR;
}

void InitTimer()
{
	TCNT0 = 0x83;	//1ms at 8MHZ
	sbi(TIMSK, 1);
	TCCR0 = 0b00000011;		//64分频
}
	
void InitIO()
{
	UCSRA = 1 << UDRE;
	UCSRB = (1 << RXEN) | (1 << TXEN) | (1 << RXCIE);
	UBRRL = 51;	//9600bps at 8MHz
	UBRRH = 0;
	UCSRC = (1 << URSEL) | (1 << UCSZ1) | (1 << UCSZ0);
	fdevopen(usart_putchar, usart_getchar, 0);
}

int main(void)
{
	CF_TInfo *I;
	char *buf;
	long i, t;
	InitIO();
	sei();
	InitTimer();
	sbi(MCUCR, SRE);
	I = (CF_TInfo *)malloc(sizeof(CF_TInfo));
	if(!I)
	{
		puts("Not enough SRAM.");
		abort();
	}

	if(CF_Initialize())
	{
		puts("Error!");
		abort();
	}

	CF_GetCardInfo(I);		//获得CF卡信息
	printf("Cylinders : %u\n", I->Cylinders);
	printf("Heads : %u\n", I->Heads);
	printf("Sectors per track : %u\n", I->SectorsPerTrack);
	printf("Total Sectors : %lu\n", I->TotalSectors);
	printf("Serial : ");
	for(i = 0; i < 20; i ++)
		putchar(I->SerialNumber[i]);
	printf("\n");
	printf("Model : ");
	for(i = 0; i < 40; i ++)
		putchar(I->ModelNumber[i]);
	printf("\n");
	printf("Total size : %lu bytes (%luMB)\n", I->TotalSize, I->TotalSize >> 20);
	printf("\n");
//读取速度测试
	buf = (char *)0x2000;	//设每次读入的扇区都读到0x2000(需要外部SRAM)
	timer = 0;
	for(i = 0; i < 2048; i += 32)		//读入开头的2048个扇区(1MB),每次读入32个
		if(CF_LBAReadSector(i, 32, buf))
		{
			puts("Error!");
			abort();
		}
	t = timer;
	printf("ReadSector Test : %luKB/s\n", 1024L * 1000 / t);
//写入速度测试
	timer = 0;
	for(i = I->LBAs - 2048; i < I->LBAs; i += 32)		//写入到最后的2048个扇区(1MB),每次写入32个
		if(CF_LBAWriteSector(i, 32, buf))
		{
			puts("Error!");
			abort();
		}
	t = timer;
	printf("WriteSector Test : %luKB/s\n", 1024L * 1000 / t);
	
	CF_Sleep();

	printf("\n");
	free(I);
	cli();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -