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

📄 demo.c

📁 Flashfile system for avr, ICC, IAR compiled with examples. very cool
💻 C
字号:
/*********************************************
Project : FlashFileSD Example
Version : 2.03
Date	: 03/11/2005
Author  : Erick M Higa    
Company : Progressive Resources LLC       
Comments: 
This is a simple example program for the FlashFileSD


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



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

#include "options.h"

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

void port_init(void)
{
	PORTA = 0xFF;		DDRA  = 0x00;
  #ifdef _MEGAAVRDEV_
	PORTB = 0xFF;		DDRB  = 0xB0 | DEMO_LED;
	PORTC = 0xFF; 		DDRC  = 0x00;	  //m103 output only
  	PORTD = 0xFF;		DDRD  = 0x00;
  #else
	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;
  #endif
}

//UART0 initialisation
// desired baud rate: 115200
// actual: baud rate:115200 (0.0%)
// char size: 8 bit
// parity: Disabled
void uart0_init(void)
{
  #ifdef _MEGAAVRDEV_
	UCSRB = 0x00;
	UCSRA = 0x00;
	UCSRC = 0x86;
	UBRRH = 0x00;
	UBRRL = 0x26;
	UCSRB = 0x18;
  #else
	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;
  #endif
}


//UART1 initialisation
// desired baud rate:115200
// actual baud rate:115200 (0.0%)
// char size: 8 bit
// parity: Disabled
  #ifndef _MEGAAVRDEV_
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;
}
#endif

//call this routine to initialise all peripherals
void init_devices(void)
{
	//stop errant interrupts until set up
	CLI(); //disable all interrupts
  #ifndef _MEGAAVRDEV_
	XDIV  = 0x00; //xtal divider
	XMCRA = 0x00; //external memory
	EICRA = 0x00; //extended ext ints
	EICRB = 0x00; //extended ext ints
	ETIMSK = 0x00; //extended timer interrupt sources
	EIMSK = 0x00;
	uart1_init();
  #endif
	port_init();
	uart0_init();
	ACSR=0x80;
	SFIOR=0x00;
	TCCR0=0x00;
	TCNT0=0x00;
	OCR0=0x00;

	// Timer/Counter 1 initialization
	// Clock source: System Clock
	// Clock value: Timer 1 Stopped
	// Mode: Normal top=FFFFh
	// OC1A output: Discon.
	// OC1B output: Discon.
	// Noise Canceler: Off
	// Input Capture on Falling Edge
	TCCR1A=0x00;
	TCCR1B=0x00;
	TCNT1H=0x00;
	TCNT1L=0x00;
	ICR1H=0x00;
	ICR1L=0x00;
	OCR1AH=0x00;
	OCR1AL=0x00;
	OCR1BH=0x00;
	OCR1BL=0x00;
	MCUCR = 0x00;
	TIMSK = 0x00; //timer interrupt sources
	SEI(); //re-enable interrupts
	//all peripherals are now initialised
}

// Declare your global variables here
#ifdef _RTC_ON_
extern unsigned char rtc_hour, rtc_min, rtc_sec;
extern unsigned char rtc_date, rtc_month;
extern unsigned int rtc_year;
#endif
#ifdef _ICCAVR_
extern char _bss_end;
#endif

#define DEMO_FILENAME	"demo.dat"
char _FF_FNAME[14];

#include <stdio.h>

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

    
 	init_devices();

  #ifdef _ICCAVR_
   #ifndef _NO_MALLOC_
   	_NewHeap(&_bss_end + 1, &_bss_end + 1001);
   #endif
	cstrcpy(_FF_FNAME, DEMO_FILENAME);
  #else
	strcpyf(_FF_FNAME, DEMO_FILENAME);
  #endif
	
  #ifdef _RTC_ON_
	twi_setup();
  #endif 

	printf("\r\nStarting the Demo!!!");

	// initialize the Secure Digital card
	while (initialize_media()==0)
	{	// Blink LED while waiting to initialize
	  #ifdef _MEGAAVRDEV_
		PORTB ^= DEMO_LED;
	  #else
		PORTD ^= DEMO_LED;
	  #endif
	}
	
  #ifdef _MEGAAVRDEV_
	PORTB &= ~DEMO_LED;
  #else
	PORTD &= ~DEMO_LED;
  #endif
	// Create File
	
	pntr1 = fcreate(_FF_FNAME, 0);
	while (pntr1==0)
	{
		if (pntr1==0)
			printf("\r\nFAILED!!! - %02X", _FF_error);
		pntr1 = fcreate(_FF_FNAME, 0);
	}

	fputc('"', pntr1);
	fprintf(pntr1, DEMO_FILENAME);
	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, (uint *)&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
	
	for(c=0; c<10; c++)
		fprintf(pntr1, "Column %02d, ", c);
	fprintf(pntr1, __CRLF);
	
	for (c=0; c<100; c++)
	{	// print numbers separated by commas
		for (n=0; n<10; n++)
			fprintf(pntr1, "%03d, ", ((c*10)+n));
		if (fputc('\r', pntr1)==EOF)
			break;			// line feed/carriage return
		if (fputc('\n', pntr1)==EOF)
			break;			// line feed/carriage return
	  #ifdef _MEGAAVRDEV_
		PORTB ^= DEMO_LED;
	  #else
		PORTD ^= DEMO_LED;
	  #endif
	}

	fclose(pntr1);

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

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

⌨️ 快捷键说明

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