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

📄 demo.c

📁 Compact Flash library for AVR
💻 C
字号:
/*********************************************
Project : FlashFileSD Example
Version : 2.00
Date    : 11/01/2004
Author  : Erick M Higa    
Company : Progressive Resources LLC       
Comments: 
This is a simple example program for the FlashFile


Chip type           : ATmega128
Program type        : Application
Clock frequency     : 14.745600 MHz
Memory model        : Small
External SRAM size  : 0
Data Stack size     : 1024
*********************************************/


#include "options.h"

#ifdef _MEGA128DEV_
  #define DEMO_LED	0x80
#else
  #define DEMO_LED	0x40
#endif

#ifndef _UART_INT_
	#define		rx_counter0		(UCSR0A & 0x80)
	#define		tx_counter0		((!UCSR0A) & 0x40)
	#define		rx_counter1		(UCSR1A & 0x80)
	#define		tx_counter1		((!UCSR1A) & 0x40)
#endif

void port_init(void)
{
	PORTA = 0xFF;		DDRA  = 0x00;
	PORTB = 0xFF;		DDRB  = 0xD0;
	PORTC = 0xFF; 		DDRC  = 0x00;	  //m103 output only
	PORTD = 0xFF;		DDRD  = DEMO_LED;
	PORTE = 0xFF;		DDRE  = 0x00;
	PORTF = 0xFF;		DDRF  = 0x00;
	PORTG = 0x1F;		DDRG  = 0x00;
}

//UART0 initialisation
// desired baud rate: 115200
// actual: baud rate:115200 (0.0%)
// char size: 8 bit
// parity: Disabled
void uart0_init(void)
{
	UCSR0B = 0x00; //disable while setting baud rate
	UCSR0A = 0x00;
	UCSR0C = 0x06;
	UBRR0L = 0x07; //set baud rate lo
	UBRR0H = 0x00; //set baud rate hi
	UCSR0B = 0x18;
}


//UART1 initialisation
// desired baud rate:115200
// actual baud rate:115200 (0.0%)
// char size: 8 bit
// parity: Disabled
void uart1_init(void)
{
	UCSR1B = 0x00; //disable while setting baud rate
	UCSR1A = 0x00;
	UCSR1C = 0x06;
	UBRR1L = 0x07; //set baud rate lo
	UBRR1H = 0x00; //set baud rate hi
	UCSR1B = 0x18;
}


//call this routine to initialise all peripherals
void init_devices(void)
{
	//stop errant interrupts until set up
	CLI(); //disable all interrupts
	XDIV  = 0x00; //xtal divider
	XMCRA = 0x00; //external memory
	port_init();
	uart0_init();
	uart1_init();

	MCUCR = 0x00;
	EICRA = 0x00; //extended ext ints
	EICRB = 0x00; //extended ext ints
	EIMSK = 0x00;
	TIMSK = 0x00; //timer interrupt sources
	ETIMSK = 0x00; //extended timer interrupt sources
	SEI(); //re-enable interrupts
	//all peripherals are now initialised
}

// Declare your global variables here
extern unsigned char rtc_hour, rtc_min, rtc_sec;
extern unsigned char rtc_date, rtc_month;
extern unsigned int rtc_year;
#ifdef _ICCAVR_
extern char _bss_end;
#endif
char flash _FF_FNAME[] = "demo.dat";

void main(void)
{
	FILE *pntr1;
	unsigned long c, n;
    unsigned char create_info[22], modify_info[22];
    unsigned char pointer;
    
 	init_devices();

	#ifdef _ICCAVR_
		_NewHeap(&_bss_end + 1, &_bss_end + 1001);
	#endif
	
	#ifdef _RTC_ON_
		twi_setup();
	#endif 


	PORTB |= 0xD0;    
	
	// initialize the Secure Digital card
	while (initialize_media()==0)
	{	// Blink LED while waiting to initialize
		PORTD ^= DEMO_LED;
	}
	PORTD &= ~DEMO_LED;

	// Create File
	
	pntr1 = fcreatec(_FF_FNAME, 0);
	while (pntr1==0)
	{
		if (pntr1==0)
			printf("\r\nFAILED!!!");
		pntr1 = fcreatec(_FF_FNAME, 0);
	}

	fputc('"', pntr1);
	fprintf(pntr1, _FF_FNAME);
	fputc('"', pntr1);
	fputc('\r', pntr1);
	fputc('\n', pntr1);

	// Write to file
	#ifdef _RTC_ON_
		// if real time clock enabled, get and print time and date to file
		rtc_get_timeNdate(&rtc_hour, &rtc_min, &rtc_sec, &rtc_date, &rtc_month, (int *)&rtc_year);
		fputc(0x22, pntr1);		// put a " in before time/date
		if ((rtc_month/10)==0)
			fputc('0', pntr1);
		fprintf(pntr1, "%d/", rtc_month);
		if ((rtc_date/10)==0)
			fputc('0', pntr1);
		fprintf(pntr1, "%d/", rtc_date);
		fprintf(pntr1, "%d  ", rtc_year);
		if ((rtc_hour/10)==0)
			fputc('0', pntr1);
		fprintf(pntr1, "%d:", rtc_hour);
			if ((rtc_min/10)==0)
			fputc('0', pntr1);
		fprintf(pntr1, "%d:", rtc_min);
		if ((rtc_sec/10)==0)
			fputc('0', pntr1);
		fprintf(pntr1, "%d", rtc_sec);
		fputc(0x22, pntr1);	    // put a " in after time/date
		fputsc("", pntr1);
	#endif
	
	#ifdef _CVAVR_
	fprintf(pntr1, "Column %d, Column %d, Column %d, Column %d, Column %d, Column %d, Column %d, Column %d, Column %d, Column %d,\r\n",
		0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
	#endif
	#ifdef _ICCAVR_
	fputsc("Column 0, Column 1, Column 2, Column 3, Column 4, Column 5, Column 6, Column 7, Column 8, Column 9,", pntr1);
	#endif
	
	
	for (c=0; c<100; c++)
	{	// print numbers separated by commas
		for (n=0; n<10; n++)
			fprintf(pntr1, "%ld, ", (((long)c*10)+(long)n));
		if (fputc('\r', pntr1)==EOF)
			break;			// line feed/carriage return
		if (fputc('\n', pntr1)==EOF)
			break;			// line feed/carriage return
		PORTD ^= DEMO_LED;
	}

	fclose(pntr1);
	
	if (fget_file_infoc(_FF_FNAME, &c, create_info, modify_info, &pointer, &n)!=EOF)
	{
		printf("\r\n%s", _FF_FNAME);
		printf("\r\n  File Size:  %ld bytes\r\n  Create Time:  ", c);
		printf("\r\n  File Size:  %ld bytes\r\n  Create Time:  ", c);
		puts(create_info);
		printf("\r  Modify Time:  ");
		puts(modify_info);
		printf("\r  Attributes:  0x%X\n", pointer);
		printf("\r  Starting Cluster:  0x%lX @ ADDR: 0x%lX\r\n", n, clust_to_addr(n));
	}
	else
		printf("\r\n - ERROR! - %X\r\n", _FF_error);

	printf("\r\n\r\nDONE!!!");
	PORTD &= ~DEMO_LED;	// Keep LED on when done
	while (1)
	{	// Blink LED when done
		PORTD ^= DEMO_LED;
		for (c=0; c<100000; c++)
			;
	};
}

⌨️ 快捷键说明

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