Vous êtes sur la page 1sur 37

Program Code of Programing device for AVT ATmega8

ProgramingDevice.c
/***************************************************** This program was produced by the CodeWizardAVR V1.25.9 Standard Automatic Program Generator Copyright 1998-2008 Pavel Haiduc, HP InfoTech s.r.l. http://www.hpinfotech.com Project : Version : Date : Author : Company : Comments: Programming device for AVR ATmega8 1.0 19/10/2008 F4CG F4CG We impress the tiem doing this prject.

Chip type : ATmega128 Program type : Application Clock frequency : 16.000000 MHz Memory model : Small External SRAM size : 0 Data Stack size : 1024 *****************************************************/ #include <mega128.h> #include <delay.h> #include <string.h> //Keypad #define A PINA.0 #define B PINA.1 #define C PINA.2 #define D PINA.3 #define OE PORTA.4 #define DA PINA.5 //GLCD #define #define #define #define #define #define #define //SPI #define #define #define #define #define #define #define #define CS1 PORTD.4 CS2 PORTD.3 RES PORTD.2 DI PORTD.5 RW PORTD.6 E PORTD.7 LCDData PORTC DDR_SPI DDRB POST_SPI PORTB DD_SS 0 DD_SCK 1 DD_MOSI 2 DD_MISO 3 ResetPort 4 SPE 6

#define #define #define #define #define

MSTR 4 SPI2X 0 SPR1 1 SPR0 0 SPIF 7

// EEPROM #define EERIE 3 #define EEMWE 2 #define EEWE 1 #define EERE 0 // SPI Programming #define SS PORTB.0 #define SCK PORTB.1 #define Reset PORTB.4 // StatusLED #define StatusLED PORTG //GLCD Charactor Constant unsigned char character[94][5] = {0b00000000,0b00000000,0b00000000,0b00000000,0b00000000,//Space 0b00000000,0b00000000,0b10011110,0b00000000,0b00000000,//! 0b00000000,0b00001110,0b00000000,0b00001110,0b00000000,//" 0b00101000,0b11111110,0b00101000,0b11111110,0b00101000,//# 0b01001000,0b01010100,0b11111110,0b01010100,0b00100100,//$ 0b01000110,0b00100110,0b00010000,0b11001000,0b11000100,//% 0b01101100,0b10010010,0b10101100,0b01000000,0b10110000,//& 0b00000000,0b10100000,0b01100000,0b00000000,0b00000000,//' 0b00000000,0b00111000,0b01000100,0b10000010,0b00000000,//( 0b00000000,0b10000010,0b01000100,0b00111000,0b00000000,//) 0b01000100,0b00101000,0b11111110,0b00101000,0b01000100,//* 0b00010000,0b00010000,0b01111100,0b00010000,0b00010000,//+ 0b00000000,0b10100000,0b01100000,0b00000000,0b00000000,//, 0b00010000,0b00010000,0b00010000,0b00010000,0b00010000,//0b00000000,0b11000000,0b11000000,0b00000000,0b00000000,//. 0b00000000,0b11000000,0b11000000,0b00000000,0b00000000,/// 0b01111100,0b10100010,0b10010010,0b10001010,0b01111100,//0 0b00000000,0b10000100,0b11111110,0b10000000,0b00000000,//1 0b10000100,0b11000010,0b10100010,0b10010010,0b10001100,//2 0b01000010,0b10000010,0b10001010,0b10010110,0b01100010,//3 0b00110000,0b00101000,0b00100100,0b11111110,0b00100000,//4 0b01001110,0b10001010,0b10001010,0b10001010,0b01110010,//5 0b01111000,0b10010100,0b10010010,0b10010010,0b01100000,//6 0b00000010,0b00000010,0b11100010,0b00011010,0b00000110,//7 0b01101100,0b10010010,0b10010010,0b10010010,0b01101100,//8 0b00001100,0b10010010,0b10010010,0b01010010,0b00111100,//9 0b00000000,0b01101100,0b01101100,0b00000000,0b00000000,//: 0b00000000,0b10101100,0b01101100,0b00000000,0b00000000,//; 0b00010000,0b00101000,0b01000100,0b10000010,0b00000000,//< 0b00101000,0b00101000,0b00101000,0b00101000,0b00101000,//= 0b00000000,0b10000010,0b01000100,0b00101000,0b00010000,//> 0b00000110,0b00000001,0b10110001,0b00010001,0b00001110,//? 0b01111100,0b10000010,0b10111010,0b10101010,0b00111100,//@ 0b11111100,0b00010010,0b00010010,0b00010010,0b11111100,//A 0b11111110,0b10010010,0b10010010,0b10010010,0b01101100,//B 0b01111100,0b10000010,0b10000010,0b10000010,0b01000100,//C 0b11111110,0b10000010,0b10000010,0b01000100,0b00111000,//D

0b11111110,0b10010010,0b10010010,0b10010010,0b10000010,//E 0b11111110,0b00010010,0b00010010,0b00010010,0b00000010,//F 0b01111100,0b10000010,0b10010010,0b10010010,0b11110100,//G 0b11111110,0b00010000,0b00010000,0b00010000,0b11111110,//H 0b00000000,0b10000010,0b11111110,0b10000010,0b00000000,//I 0b01000000,0b10000010,0b10000010,0b01111110,0b00000010,//J 0b11111110,0b00010000,0b00101000,0b01000100,0b10000010,//K 0b11111110,0b10000000,0b10000000,0b10000000,0b10000000,//L 0b11111110,0b00000100,0b00011000,0b00000100,0b11111110,//M 0b11111110,0b00001000,0b00010000,0b00100000,0b11111110,//N 0b01111100,0b10000010,0b10000010,0b10000010,0b01111100,//O 0b11111110,0b00010010,0b00010010,0b00010010,0b00001100,//P 0b01111100,0b10000010,0b10100010,0b01000010,0b10111100,//Q 0b11111110,0b00010010,0b00110010,0b01010010,0b10001100,//R 0b01001100,0b10010010,0b10010010,0b10010010,0b01100100,//S 0b00000010,0b00000010,0b11111110,0b00000010,0b00000010,//T 0b01111110,0b10000000,0b10000000,0b10000000,0b01111110,//U 0b00111110,0b01000000,0b10000000,0b01000000,0b00111110,//V 0b01111110,0b10000000,0b01110000,0b10000000,0b01111110,//W 0b11000110,0b00101000,0b00010000,0b00101000,0b11000110,//X 0b00000110,0b00001000,0b11110000,0b00001000,0b00000110,//Y 0b11000010,0b10100010,0b10010010,0b10001010,0b10000110,//Z 0b11111110,0b10000010,0b10000010,0b00000000,0b00000000,//[ 0b00000000,0b00000000,0b10000010,0b10000010,0b11111110,//] 0b00000100,0b00001000,0b00010000,0b00100000,0b01000000,//' 0b00001000,0b00000100,0b00000010,0b00000100,0b00001000,//^ 0b10000000,0b10000000,0b10000000,0b10000000,0b10000000,//_ 0b00000000,0b00000000,0b00000110,0b00000000,0b00000000,//' 0b01000000,0b10101000,0b10101000,0b10101000,0b11110000,//a 0b11111110,0b10001000,0b10001000,0b10001000,0b01110000,//b 0b01110000,0b10001000,0b10001000,0b10001000,0b10001000,//c 0b01110000,0b10001000,0b10001000,0b10001000,0b11111110,//d 0b01110000,0b10101000,0b10101000,0b10101000,0b00110000,//e 0b00001000,0b00001000,0b11111100,0b00001010,0b00001010,//f 0b00010000,0b10101000,0b10101000,0b10101000,0b01111000,//g 0b11111110,0b00010000,0b00001000,0b00001000,0b11110000,//h 0b00000000,0b10001000,0b11111010,0b10000000,0b00000000,//i 0b01000000,0b10000000,0b10001000,0b01111010,0b00000000,//j 0b00000000,0b11111110,0b00100000,0b01010000,0b10001000,//k 0b00000000,0b10000010,0b11111110,0b10000000,0b00000000,//l 0b11111000,0b00001000,0b11110000,0b00001000,0b11110000,//m 0b11111000,0b00010000,0b00001000,0b00001000,0b11110000,//n 0b01110000,0b10001000,0b10001000,0b10001000,0b01110000,//o 0b11111000,0b00101000,0b00101000,0b00101000,0b00010000,//p 0b00010000,0b00101000,0b00101000,0b00101000,0b11111000,//q 0b00000000,0b11111000,0b00010000,0b00001000,0b00001000,//r 0b10010000,0b10101000,0b10101000,0b10101000,0b01000000,//s 0b00001000,0b00001000,0b01111110,0b10001000,0b10001000,//t 0b01111000,0b10000000,0b10000000,0b01000000,0b11111000,//u 0b00111000,0b01000000,0b10000000,0b01000000,0b00111000,//v 0b01111000,0b10000000,0b01100000,0b10000000,0b01111000,//w 0b10001000,0b01010000,0b00100000,0b01010000,0b10001000,//x 0b00011000,0b10100000,0b10100000,0b10100000,0b01111000,//y 0b10001000,0b11001000,0b10101000,0b10011000,0b10001000,//z 0b00000000,0b00010000,0b01101100,0b10000010,0b10000010,//{ 0b00000000,0b00000000,0b11111110,0b00000000,0b00000000,//| 0b10000010,0b10000010,0b01101100,0b00010000,0b00000000};//}

//Device code and name unsigned char deviceName[17][10] = {"tiny12","tiny15","S1200","S2313","S2323","S2333","S2343","S4414","S4433","S4434", "S8515","S8535","mega32","mega83","mega103","mega161","mega163"}; unsigned char deviceCode[17][2] = {0x90,0x05,0x90,0x06,0x90,0x01,0x91,0x01,0x91,0x02,0x91,0x05,0x91,0x03,0x92,0x01,0x 92,0x03,0x92,0x02,0x93,0x01,0x93,0x03,0x95,0x01,0x93,0x05,0x97,0x01,0x94,0x01,0x94, 0x02}; //GLCD //Integer to Ascii, not used void IToA(unsigned char num,unsigned char *temp,unsigned char base) { if(base == 16) { if(num<10) { temp[0] = num+48; } else { temp[0] = num+55; } temp[1] = '\0'; } } //Print flash text on GLCD void LCDPrintFlash(flash char *data,unsigned char flag) { unsigned char i = 0; unsigned char j = 0; i = 0; DI = 1; while(data[j] != '\0') { i = 0; while(i < 5) { LCDData = character[((data[j])-32)][i]; if(flag) LCDData = 0xff-LCDData; E = 1; E = 0; delay_ms(1); i++; } LCDData = 0x00; if(flag) LCDData = 0xff-LCDData; E = 1; E = 0; delay_ms(1); j++; }

//Goto XY void LCDGotoXY(unsigned char page, unsigned char y, unsigned char CS) { unsigned char init[2]; unsigned char i = 0; DI = 0; if(CS == 0) { CS1 = 1; CS2 = 0; } else { CS1 = 0; CS2 = 1; } init[0] = 0xB8 + page; init[1] = 0x40 + (y*6); if(CS==0) { init[1] += 4; } while(i < 2) { LCDData = init[i]; E = 1; E = 0; delay_ms(1); i++; }

//Clear GLCD by write space on evey area void LCDClear(void) { unsigned char i = 0; unsigned char j = 0; CS1 = 1; CS2 = 1; while(j < 8) { DI = 0; LCDData = 0xB8 + j; E = 1; E = 0; delay_ms(1); i++; i=0; DI = 1; while(i < 64) { LCDData = 0x00; CS2 = 1; E = 1; E = 0; delay_ms(1); i++;

} j++; } LCDGotoXY(0,0,0); } //Init GLCD void LCDInit(void) { unsigned char init[4]; unsigned char i = 0; RES = 1; RW = 0; DI = 0; CS1 = 1; CS2 = 1; init[0] = init[1] = init[2] = init[3] =

0x3F; 0xC0; 0x40; 0xB8;

//Turn On //Z address //Y address //X address (page)

while(i<4) { LCDData = init[i]; E = 1; E = 0; delay_ms(1); i++; } CS2 = 0; LCDClear(); } //Keypad //Wait until any key press and return that key unsigned char GetKey(void) { unsigned char temp; temp = 0x00; //Enable key scan OE = 0; temp = 0x00; while(!DA); if(A) { if(B) { if(C) {

if(D) { } else

//Return temp = 0x0F;

{ } } else {

//0 temp = 0x00;

if(D) { } else { }

//> temp = 0x0B;

//< temp = 0x0A; } } else {

if(C) {

if(D) { } else { }

//^ temp = 0x0D;

//5 temp = 0x05; } else {

if(D) { //6 temp = 0x06; } else { } } } else { }

//4 temp = 0x04;

if(B) {

if(C) {

if(D) {

//V temp = 0x0E;

} else { } } else {

//8 temp = 0x08;

if(D) { } else { }

//9 temp = 0x09;

//7 temp = 0x7; } } else {

if(C) {

if(D) { } else { }

//Set temp = 0x0C;

//2 temp = 0x2; } else {

if(D) { //3 temp } else { } } } delay_ms(300); } = 0x03;

//1 temp = 0x01;

//Disable key scan OE = 1; return(temp); }

//SPI //Enable SPI for chip programming void SPI_MasterInit(void) { // Enable SPI, Master, set clock rate fck/128 SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR1)|(1<<SPR0); } //SPI transmit void SPI_MasterTransmit(char cData) { // Start transmission SPDR = cData; // Wait for transmission complete while(!(SPSR & (1<<SPIF))); } //SPI receive char SPI_SlaveReceive(void) { // Wait for reception complete while(!(SPSR & (1<<SPIF))); // Return data register return(SPDR); } //EEPROM //Write data into EEPROM void EEPROM_write(unsigned int uiAddress, unsigned char ucData) { // Wait for completion of previous write while(EECR & (1<<EEWE)) ; // Set up address and data registers EEAR = uiAddress; EEDR = ucData; // Write logical one to EEMWE EECR |= (1<<EEMWE); // Start eeprom write by setting EEWE EECR |= (1<<EEWE); } //Read data from EEPROM unsigned char EEPROM_read(unsigned int uiAddress) { // Wait for completion of previous write while(EECR & (1<<EEWE)) ; // Set up address register EEAR = uiAddress; // Start eeprom read by writing EERE EECR |= (1<<EERE); // Return data from data register return EEDR; }

//SPI Programing //Use flash LED to show response of target void Response(flash unsigned char *data) { unsigned char i = 0; DDRG = 0xff; while(data[i] != '\0') { delay_ms(100); if(data[i] == '0') { PORTG =0x00; } else { PORTG =0x01; } i++; } } //Send programming enable command to the target unsigned char SPI_Programming_Init(void) { unsigned char temp = 0x00; //Power-up sequence Reset = 0; SCK = 0; //Given Reset = Reset = Reset = Reset = Reset = Reset = Reset = possitive pulse of at lease two CPU clock cycle duration 0; 1; 1; 1; 1; 1; 0;

//Wait at lease 20 ms delay_ms(100); //Send programing enable command SPI_MasterTransmit(0xAC); SPI_MasterTransmit(0x53); SPI_MasterTransmit(0x00); temp = SPI_SlaveReceive(); SPI_MasterTransmit(0x00); //Check response from target if(temp == 0x53) { return(0xFF); } else { return(0x00); } }

//Read vendor code, part family, flash size, and part number //of the target chip unsigned char ReadDeviceCode(void) { unsigned char vendorCode = 0x00; unsigned char partFamilyAndFlashSize = 0x00; unsigned char partNumber = 0x00; unsigned char i = 0x00; //Read device code SPI_MasterTransmit(0x30); SPI_MasterTransmit(0x00); SPI_MasterTransmit(0x00); SPI_MasterTransmit(0x30); vendorCode = SPI_SlaveReceive(); //Read part family and memory size SPI_MasterTransmit(0x30); SPI_MasterTransmit(0x00); SPI_MasterTransmit(0x01); SPI_MasterTransmit(0x30); partFamilyAndFlashSize = SPI_SlaveReceive(); //Read part family and memory size SPI_MasterTransmit(0x30); SPI_MasterTransmit(0x00); SPI_MasterTransmit(0x02); SPI_MasterTransmit(0x30); partNumber = SPI_SlaveReceive(); if(vendorCode == 0x1E) { while(1) { if(deviceCode[i][0] == partFamilyAndFlashSize & deviceCode[i][0] == partNumber) break; i++; } return(i); } else { return(0xFF); } } //Read flash high and low byte unsigned int ReadFlash(unsigned int address) { unsigned char addressH; unsigned char addressL; unsigned char flashH; unsigned char flashL; unsigned int data = 0x0000; addressH = address>>8; addressL = address;

//Read flash high byte SPI_MasterTransmit(0x28); SPI_MasterTransmit(addressH); SPI_MasterTransmit(addressL); SPI_MasterTransmit(0x00); flashH = SPI_SlaveReceive(); //Read flash low byte SPI_MasterTransmit(0x20); SPI_MasterTransmit(addressH); SPI_MasterTransmit(addressL); SPI_MasterTransmit(0x00); flashL = SPI_SlaveReceive(); data = flashL; data = data<<8; data += flashH; return(data); } //Write flash, using EEPROM data void WritePageFlash(void) { unsigned int i = 0x00; unsigned int j = 0x00; //Write only 128 page for(i=0;i<128;i++) { //Write data in tempororary page for(j=0;j<32;j++) { //Low byte SPI_MasterTransmit(0x40); SPI_MasterTransmit(0x00); SPI_MasterTransmit(j); SPI_MasterTransmit(EEPROM_read((i*0x0040)+(j*2))); //High byte SPI_MasterTransmit(0x48); SPI_MasterTransmit(0x00); SPI_MasterTransmit(j); SPI_MasterTransmit(EEPROM_read((i*0x0040)+(j*2)+1));

//Write temporary page into the real flash SPI_MasterTransmit(0x4C); SPI_MasterTransmit(i>>3); SPI_MasterTransmit(i<<5); SPI_MasterTransmit(0x00); //Show status delay_ms(5); StatusLED = 1; delay_ms(5); StatusLED = 0; } }

//Check data in flash compare to the data in EEPROM //Return 0xFF if all data are correct //and 0x00 if some data is not correct unsigned char VerifyFlash(void) { unsigned int address; unsigned char data; for(address=0;address<4096;address++) { //Low byte SPI_MasterTransmit(0x20); SPI_MasterTransmit(address>>8); SPI_MasterTransmit(address); SPI_MasterTransmit(0x00); data = SPI_SlaveReceive(); if(EEPROM_read((2*address)) != data) { return(0x00); } //High byte SPI_MasterTransmit(0x28); SPI_MasterTransmit(address>>8); SPI_MasterTransmit(address); SPI_MasterTransmit(0x00); data = SPI_SlaveReceive(); if(EEPROM_read((2*address)+1) != data) { return(0x00); }

} return(0xFF);

//Write target EEPROM data void WriteEEPROM(void) { unsigned int address; for(address=0;address<512;address++) { SPI_MasterTransmit(0xC0); SPI_MasterTransmit(address>>8); SPI_MasterTransmit(address); SPI_MasterTransmit(EEPROM_read(address)); delay_ms(5); StatusLED = 1; delay_ms(5); StatusLED = 0;

} }

//Check data in target EEPROM compare to the data in EEPROM //Return 0xFF if all data are correct //and 0x00 if some data is not correct unsigned char VerifyEEPROM(void) {

unsigned int address; unsigned int data; for(address=0;address<512;address++) { SPI_MasterTransmit(0xa0); SPI_MasterTransmit(address>>8); SPI_MasterTransmit(address); SPI_MasterTransmit(0x00); data = SPI_SlaveReceive(); if(EEPROM_read(address) != data) { return(0x00); } delay_ms(5); StatusLED = 1; delay_ms(5); StatusLED = 0;

} return(0xFF);

//Erease the target chip void ChipErease(void) { SPI_MasterTransmit(0xAC); SPI_MasterTransmit(0x80); SPI_MasterTransmit(0x04); SPI_MasterTransmit(0x00); delay_ms(100); } //Menu void ShowTitle(void) { LCDClear(); LCDGotoXY(2,0,0); LCDPrintFlash(" Programme",0); LCDGotoXY(2,0,1); LCDPrintFlash("r Device",0); LCDGotoXY(4,0,0); LCDPrintFlash(" For AVR",0); LCDGotoXY(4,0,1); LCDPrintFlash(" ATmega8",0); } void ShowMenu(void) { LCDClear(); LCDGotoXY(1,0,0); LCDPrintFlash(" ME",0); LCDGotoXY(1,0,1); LCDPrintFlash("NU",0); LCDGotoXY(3,0,0); LCDPrintFlash(" > Progra",0); LCDGotoXY(3,0,1);

LCDPrintFlash("m Flash",0); LCDGotoXY(4,0,0); LCDPrintFlash(" Progra",0); LCDGotoXY(4,0,1); LCDPrintFlash("m EEPROM",0); LCDGotoXY(5,0,0); LCDPrintFlash(" Comman",0); LCDGotoXY(5,0,1); LCDPrintFlash("d 3",0); LCDGotoXY(6,0,0); LCDPrintFlash(" Comman",0); LCDGotoXY(6,0,1); LCDPrintFlash("d 4",0);

void main(void) { // Declare your local variables here unsigned char SPI_enable = 0x00; unsigned char enableLine = 0x00; unsigned char key = 0x00; // Input/Output Ports initialization // Port A initialization // Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In // State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T PORTA=0x00; DDRA=0x10; // Port B initialization // Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In // State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T PORTB=0x00; DDRB=0x17; // Port C initialization // Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In // State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T PORTC=0x00; DDRC=0xFF; // Port D initialization // Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In // State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T PORTD=0x00; DDRD=0xFF; // Port E initialization // Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In // State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T PORTE=0x00; DDRE=0x00; // Port F initialization // Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In // State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T PORTF=0x00; DDRF=0x00;

// Port G initialization // Func4=In Func3=In Func2=In Func1=In Func0=In // State4=T State3=T State2=T State1=T State0=T //Show title LCDInit(); LCDClear(); ShowTitle(); //Wait for key press GetKey(); while(1) { //Show menu LCDClear(); ShowMenu(); enableLine = 0x03; //Get key key = 0x00; //Wait for select a menu while(!(key==0x0F & (enableLine == 0x03 | enableLine == 0x04))) { key = GetKey(); //Shift cursor down if(key == 0x0E & enableLine < 0x06) { LCDGotoXY(enableLine,0,0); LCDPrintFlash(" ",0); enableLine++; LCDGotoXY(enableLine,0,0); LCDPrintFlash(" >",0); } //Shift cursor up if(key == 0x0D & enableLine > 0x03) { LCDGotoXY(enableLine,0,0); LCDPrintFlash(" ",0); enableLine--; LCDGotoXY(enableLine,0,0); LCDPrintFlash(" >",0); } } // Start flash programming if(enableLine == 0x03) { //Programming init SPI_MasterInit(); LCDClear(); LCDGotoXY(1,0,0); LCDPrintFlash("Starting",0); Response("101010101"); SPI_enable=0x00;

//Enter prgramming mode while(SPI_enable==0x00) { SPI_enable = SPI_Programming_Init(); Response("11110000"); if(SPI_enable==0x00) { LCDClear(); LCDGotoXY(2,0,0); LCDPrintFlash("Starting fail.",0); Response("101010101"); } } //Chip Erease LCDGotoXY(2,0,0); LCDPrintFlash("Ereasing",0); ChipErease(); Response("101010101"); //Writing LCDGotoXY(3,0,0); LCDPrintFlash("Writing",0); WritePageFlash(); Response("101010101"); //Verifying LCDGotoXY(4,0,0); LCDPrintFlash("Verifying",0); if(VerifyFlash() == 0xFF) { LCDGotoXY(5,0,0); LCDPrintFlash("Programmin",0); LCDGotoXY(5,0,1); LCDPrintFlash("g Success ",0); Response("101010101"); } else { LCDGotoXY(5,0,0); LCDPrintFlash("Programmin",0); LCDGotoXY(5,0,1); LCDPrintFlash("g Fail ",0); Response("101010101"); } //Finished Reset = 1; delay_ms(500); } // Start EEPROM programming if(enableLine == 0x04) { //Programming init SPI_MasterInit(); LCDClear(); LCDGotoXY(1,0,0);

LCDPrintFlash("Starting",0); Response("101010101"); SPI_enable=0x00; //Enter programming mode while(SPI_enable==0x00) { SPI_enable = SPI_Programming_Init(); Response("11110000"); if(SPI_enable==0x00) { LCDClear(); LCDGotoXY(2,0,0); LCDPrintFlash("Starting fail.",0); Response("101010101"); } } //Writing LCDGotoXY(2,0,0); LCDPrintFlash("Writing",0); WriteEEPROM(); Response("101010101"); //Verifying LCDGotoXY(3,0,0); LCDPrintFlash("Verifying",0); if(VerifyEEPROM() == 0xFF) { LCDGotoXY(4,0,0); LCDPrintFlash("Programmin",0); LCDGotoXY(4,0,1); LCDPrintFlash("g Success ",0); Response("101010101"); } else { LCDGotoXY(4,0,0); LCDPrintFlash("Programmin",0); LCDGotoXY(4,0,1); LCDPrintFlash("g Fail ",0); Response("101010101"); } //Finished Reset = 1; delay_ms(500); }; } }

Lee.c
#include #include #include #include #include #include #include #include #define #include <avr/io.h> <avr/signal.h> <avr/interrupt.h> <avr/wdt.h> <stdio.h> <stdarg.h> <string.h> <stdlib.h> F_CPU 16000000 <util/delay.h>

#include "lcd.h" #include "mydefs.h" #include "util.h" #include "fatfile/fatfile.h" //------------------------------------------------------------------------// char mmc_card_present (void) { MMC_CARD_PORT |= _BV (MMC_CARD_DET); // enable card detection, pull-up MMC_CARD_DDR &= ~_BV (MMC_CARD_DET); if (bit_is_clear(MMC_CARD_INP, MMC_CARD_DET)) return 0; // card detected return 1; // have card } //------------------------------------------------------------------------// unsigned char menu (void) { unsigned char choice; printf printf printf printf printf ("\r\n"); ("\r\n(1) Init ("\r\n(2) Read ("\r\n(3) Read ("\r\n\nPlease card"); File"); Card information"); select ?");

while (1) { choice = ser_getc (); if ((choice > '0') && (choice < '4')) { break; } _delay_ms (1000); }; return choice;

//------------------------------------------------------------------------// //-------------------------------------------------------------------------// unsigned char initCard (void) { unsigned char initCardFlag = 0;

if (!mmc_card_present ()) { if(GetDriveInformation()!=F_OK) // get drive parameters { printf ("\r\nCard Fail"); initCardFlag = 0; LED_PORT &= ~_BV(LED); } else { // Card OK printf ("\r\nCard OK\n"); initCardFlag = 1; LED_PORT |= _BV(LED); } } else { printf ("\r\nNo Card"); initCardFlag = 0; } return initCardFlag; } //------------------------------------------------------------------------// void ReadCardInfo (void) { LED_PORT |= _BV(LED); if(Findfirst()!=0) //Finde erste Datei/Verzeichnis im aktuellen Verzeichnis { do { printf("%s ",ffblk.ff_name); //8.3 DOS Name if(ffblk.ff_attr==ATTR_FILE) printf("% 9lu\n",ffblk.ff_fsize); if(ffblk.ff_attr==ATTR_DIRECTORY) printf("<DIR>\n"); if(ffblk.ff_longname[0]!=0) printf("%s\n",ffblk.ff_longname); } while (Findnext()!=0); } else { printf("No file/dir found !\n"); } } //------------------------------------------------------------------------// //------------------------------------------------------------------------// //------------------------------------------------------------------------// //------------------------------------------------------------------------// //------------------------------------------------------------------------// int main(void) { DDRG |= _BV(LED);

SFIOR&=!(1<<PUD); ser_init();

init_uart1 (); sei(); LED_PORT #ifdef LCD init_lcd (); lcd_gotoxy (1,1); lcd_printf ("SD CARD"); lcd_gotoxy (2,1); //LCD printf ("\r\nSD card test"); if (!mmc_card_present ()) { printf ("\r\nCard Ready...."); if(GetDriveInformation()!=F_OK) // get drive parameters { #ifdef #endif } else { #ifdef #endif } else { LCD } LCD LCD lcd_gotoxy (2,1); lcd_printf ("\r\nCard Fail"); printf ("\n\rCard Fail"); &= ~_BV(LED);

#endif

LED_PORT

|= _BV(LED);

lcd_gotoxy (2,1); lcd_printf ("\r\nSD/MMC Ready"); printf ("\r\nCard OK\n");

#ifdef #endif

lcd_gotoxy (2,1); lcd_printf ("\r\nNo card"); printf ("\r\nNO Card");

unsigned char key; while (1) { #ifdef #endif DEBUG key = menu (); printf ("\r\nKey = %c",key); switch (key) { case '1' :

//init card

initCard (); break; case '2' : //Read card if (initCard ()) { LED_PORT |= _BV(LED); printf ("\r\nRead Files TEST.TXT"); printf ("\r\nDetail in File : "); ReadFileRaw ("TEST.TXT"); printf ("\r\n"); } else { printf ("\r\nPlease select 1 LED_PORT } break; &= ~_BV(LED);

before");

case '3' :

Information\r\n");

if (initCard ()) { printf ("\r\nGet Card ReadCardInfo (); printf ("\r\n"); } break; default : //none value break;

} }; } while (1);

mydefs.h
#ifndef __MYDEFS_H #define __MYDEFS_H #define F_16 #define F_CPU #define #define #define LCD LED LED_PORT PORTG PG0 16000000 //processor clock

#endif //__MYDEFS_H

util.h
#include <avr/io.h> #include <avr/signal.h> #include <avr/interrupt.h> #define NOP() asm volatile ("nop" ::) #define sbi(portn, bitn) ((portn)|=(1<<(bitn))) #define cbi(portn, bitn) ((portn)&=~(1<<(bitn)))

dir.h
#ifndef __DIR_H #define __DIR_H extern extern extern extern unsigned unsigned unsigned unsigned char char char char Mkdir(char *name); Chdir(char *name); MakeNewFileEntry(char fileid); UpdateFileEntry(char fileid);

extern unsigned char ScanRootDir(char fileid); extern unsigned char ScanOneDirectorySector(char fileid, unsigned long sector); extern unsigned char SearchRootDir(char fileid); #ifdef USE_FAT32 extern unsigned char SearchSubDir(char fileid, unsigned long cluster); extern unsigned char ScanSubDir(char fileid, unsigned long startcluster); #else extern unsigned char SearchSubDir(char fileid, unsigned int cluster); extern unsigned char ScanSubDir(char fileid, unsigned int startcluster); #endif extern unsigned char SearchDirSector(char fileid, unsigned long sector); extern void MakeFileName(char *inname, char fileid); #define ls() #endif //__DIR_H FindName(-1)

dos.h
#ifndef __DOS_H #define __DOS_H #define MAX_OPEN_FILE ((char) 1) // every 1 declared open file takes 558 Bytes for FAT12/16/32 // every 1 declared open file takes 554 Bytes for FAT12/16 only // ATMega32 has only 2kB RAM. If you want to use 2 open files, you // have to switch off the FAT buffer ! //#define SMALL_FWRITE #define FAST_FWRITE //#define SMALL_FREAD #define FAST_FREAD // less code but slower // more code but faster // less code but slower // more code but faster

// security check ! #if defined (SMALL_FWRITE) && defined (FAST_FWRITE) #error "Define SMALL_FWRITE or FAST_FWRITE only in dos.h, NOT both !" #endif #if !defined (SMALL_FWRITE) && !defined (FAST_FWRITE) #error "Define at least SMALL_FWRITE or FAST_FWRITE in dos.h !" #endif #if defined (SMALL_FREAD) && defined (FAST_FREAD) #error "Define SMALL_FREAD or FAST_FREAD only in dos.h, NOT both !" #endif #if !defined (SMALL_FREAD) && !defined (FAST_FREAD) #error "Define at least SMALL_FREAD or FAST_FREAD in dos.h !" #endif // Some more defines for special filesystem handling #define STRICT_FILESYSTEM_CHECKING // If you define this, some very rare special cases will be // noticed. But this needs more code. // Special cases when files where made on a Win system and // copied to the flash card: // // Bug found by Michele Ribaudo // Files with zero filesize have no clusters allocated ! // Calling Remove() and Fopen() may hang up the system. // If you define STRICT_FILESYSTEM_CHECKING opening a // zero length file for reading or writing gives an error. // You can remove a zero length file ! // // You don't need this define if all files where made with // my FAT system. Even if filesize is zero. // If unsure keep it defined ! //fopen flags #define F_CLOSED 0

#define F_READ #define F_WRITE #define F_ERROR #define F_OK

1 2 0 // dir/file operation failed 1 // dir/file operation successfull

// #undef defines below in "mydefs.h" if you don't need them // spare program memory by deciding if we want to read, write or both #define DOS_READ //define this if you want to read files #define DOS_WRITE //define this if you want to write files #define DOS_DELETE //define this if you want to delete files #define DOS_READ_RAW //define this if you want to read files with ReadFileRaw() #define DOS_CHDIR //define this if you want to go into subdirectories #define DOS_MKDIR //define this if you want to make subdirectories // spare program memory by deciding if we want to use FAT12, FAT16, FAT32. // don't touch if you don't know the FAT type of your drive ! #define USE_FAT12 //define this if you want to use FAT12 #define USE_FAT16 //define this if you want to use FAT16 #define USE_FAT32 //define this if you want to use FAT32 #define USE_FATBUFFER //define this if you want to use a FAT buffer //needs 517 Bytes of RAM !

#define USE_FINDFILE //define this if you want to use Findfirst(); Findnext(); #define USE_FINDLONG //define this if you want to get long filenames //from Findfirst(); Findnext(); extern char Fopen(char *name, unsigned char flag); extern void Fclose(char fileid); extern unsigned int Fread(unsigned char *buf, unsigned int count, char fileid ); extern unsigned int Fwrite(unsigned char *buf, unsigned int count, char fileid); extern extern extern extern void Fflush(char fileid); void fflush_all(void); unsigned char Remove(char *name); unsigned long Filelength(char fileid);

extern unsigned char ReadFileRaw(char *name); extern unsigned char FindName(char fileid); extern char findfreefiledsc(void); // return -1 if too many open files //this is for easier and faster converting from byte arrays to UINT, ULONG //ui and ul share the same memory space union Convert { unsigned int ui; unsigned long ul; }; struct DateTime { // Default if you not have unsigned unsigned unsigned unsigned unsigned time int day; int date; int month; int year; // 2007 (1980+27); == 2007 int hour;

unsigned int min; unsigned int sec; }; struct DateTime RTC; unsigned char updatefiles; #ifdef MMC_CARD_SPI #include "mmc_spi.h" #endif #include "fat.h" #include "dir.h" #ifdef USE_FINDFILE #include "find_x.h" #endif #include "drivefree.h" #endif //__DOS_H

drivefree.h
#ifndef __DRIVEFREE_H #define __DRIVEFREE_H extern unsigned long drivefree(void); extern unsigned long driveused(void); extern unsigned long drivesize(void); #endif // __DRIVEFREE_H

fat.h
#ifndef __FAT_H #define __FAT_H //#define FAT_DEBUG //activate output via printf() or puts() //file operations #define END_DIR 0 #define NO_MATCH 1 #define MATCH_NAME 2 #define MATCH_EXT 3 #define FULL_MATCH MATCH_NAME + MATCH_EXT #define PART1_TABLE_OFFSET 0x01BE //offset to first partitiontable in sector 0 //Using structures needs less memory than indexing in arrays like inbuff[] //partitiontable structure //most of it is not used in this program //bootsector offset is the only thing we need //because C/H/S values are not used. LBA ! struct PartInfo { = inactive partition unsigned char status; //Partition status, 0x80 = Active, 0x00 unsigned char firsthead; //First head used by partition unsigned int firstseccyl; //First sector and cylinder used by unsigned char type; unsigned char lasthead; unsigned int lastseccyl; unsigned long bootoffset; unsigned long secofpart; //Partition type //Last head used by partition //Last sector and cylinder used by //Location of boot sector. !!!!!!!!!!! //Number of sectors for partition

partition };

//first sector of disc is the master boot record //it contains four partitiontables //only the first partition is used in this program struct MBR { unsigned char dummy[PART1_TABLE_OFFSET]; //we don't need all these bytes struct PartInfo part1; struct PartInfo part2; struct PartInfo part3; struct PartInfo part4; //all bytes below are not necessary }; //part of FAT12/16 bootsector different to FAT32 struct RemBoot //FAT12/16 defs beginning at offset 36 { unsigned char BS_DrvNum; unsigned char BS_Reserved1; unsigned char BS_BootSig; unsigned char BS_VolID[4]; char BS_VolLab[11]; char BS_FilSysType[8]; unsigned char remaining_part[450]; };

//part of FAT32 bootsector different to FAT12/16 struct RemBoot32 //FAT32 defs beginning at offset 36 { unsigned long BPB_FATSz32; //4 bytes unsigned int BPB_ExtFlags; //2 bytes unsigned int BPB_FSVer; //2 bytes unsigned long BPB_RootClus; //4 bytes unsigned int BPB_FSInfo; //2 bytes unsigned int BPB_BkBootSec; //2 bytes unsigned char BPB_Reserved[12]; unsigned char BS_DrvNum; unsigned char BS_Reserved1; unsigned char BS_BootSig; unsigned long BS_VolID; //4 bytes char BS_VolLab[11]; char BS_FilSysType[8]; unsigned char remaining_part[422]; }; union endboot { struct RemBoot rm; struct RemBoot32 rm32; }; struct BootSec { unsigned char BS_jmpBoot[3]; char BS_OEMName[8]; unsigned int BPB_BytesPerSec; unsigned char BPB_SecPerClus; unsigned int BPB_RsvdSecCnt; unsigned char BPB_NumFATs; unsigned int BPB_RootEntCnt; unsigned int BPB_TotSec16; unsigned char BPB_Media; unsigned int BPB_FATSz16; unsigned int BPB_SecPerTrk; unsigned int BPB_NumHeads; unsigned long BPB_HiddSec; unsigned long BPB_TotSec32; union endboot eb; }; #define BYTE_PER_SEC #define FAT12 #define FAT16 #define FAT32 12 16 32 (unsigned int)512

//3 byte //8 byte //2 bytes //1 //2 bytes //1 //2 bytes //2 bytes //1 //2 bytes //2 bytes //2 bytes //4 bytes //4 bytes //remaining part of bootsector

//defines for special cluster values //free cluster has value 0 //for fat32 don't use upper four bits ! ignore them //cluster value of 0x10000000 is a FREE cluster in FAT32 //values for end of cluster chain //ranges for example for FAT12 from 0xFF8 to 0xFFF #define EOC12 #define EOC16 0xFF8 0xFFF8

#define EOC32

0x0FFFFFF8

//values for bad marked clusters #define BADC12 0xFF7 #define BADC16 0xFFF7 #define BADC32 0x0FFFFFF7 //values for reserved clusters //ranges for example for FAT12 from 0xFF0 to 0xFF6 #define RESC12 0xFF0 #define RESC16 0xFFF0 #define RESC32 0x0FFFFFF0 #ifdef USE_FAT32 #define DISK_FULL 0xFFFFFFFF #else #define DISK_FULL 0xFFFF #endif //File/Dir Attributes #define ATTR_FILE #define ATTR_READ_ONLY #define ATTR_HIDDEN #define ATTR_SYSTEM #define ATTR_VOLUME_ID #define ATTR_DIRECTORY #define ATTR_ARCHIVE #define ATTR_LONG_NAME #define ATTR_NO_ATTR 0x00 //not defined by MS ! I did it 0x01 0x02 0x04 0x08 0x10 0x20 0x0F 0xFF //not defined by MS ! I did it

//Char codes not allowed in a filename //NOT checked yet //0x22, 0x2A, 0x2B, 0x2C, 0x2E, 0x2F, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F, 0x5B, 0x5C, 0x5D, and 0x7C. struct DirEntry { unsigned unsigned unsigned unsigned unsigned char char char char char DIR_Name[8]; DIR_Ext[3]; DIR_Attr; DIR_NTres; DIR_CrtTimeTenth; DIR_CrtTime; DIR_CrtDate; DIR_LastAccDate; DIR_FstClusHI; DIR_WrtTime; DIR_WrtDate; DIR_FstClusLO; DIR_FileSize; //8 chars filename //3 chars extension //file attributes RSHA //set to zero //creation time part in //creation time //creation date //last access date (no time for //first cluster high word //last write time //last write date //first cluster low word

milliseconds

this !)

unsigned int unsigned int unsigned int unsigned unsigned unsigned unsigned unsigned }; int int int int long

//do a little trick for getting long name characters from a DirEntry //DirEntryBuffer later gets the same adress as DirEntry struct DirEntryBuffer { unsigned char longchars[sizeof(struct DirEntry)]; };

//Prototypes extern unsigned char GetDriveInformation(void); extern void UpdateFATBuffer(unsigned long newsector); #ifdef USE_FAT32 extern unsigned extern unsigned extern unsigned number); extern unsigned extern unsigned #else extern unsigned extern unsigned extern unsigned number); extern unsigned extern unsigned #endif #ifdef USE_FAT32 extern unsigned extern unsigned extern unsigned #else extern unsigned extern unsigned #endif long GetFirstSectorOfCluster(unsigned long n); long GetNextClusterNumber(unsigned long cluster); char WriteClusterNumber(unsigned long cluster, unsigned long long AllocCluster(unsigned long currentcluster); long FindFreeCluster(unsigned long currentcluster); long GetFirstSectorOfCluster(unsigned int n); int GetNextClusterNumber(unsigned int cluster); char WriteClusterNumber(unsigned int cluster, unsigned int int AllocCluster(unsigned int currentcluster); int FindFreeCluster(unsigned int currentcluster);

long endofclusterchain; long maxcluster; // last usable cluster+1 long FAT32RootCluster; int endofclusterchain; int maxcluster; // last usable cluster+1 // last sector on drive

extern unsigned long maxsect; extern unsigned char secPerCluster; extern unsigned long BytesPerCluster; extern unsigned char dirbuf[]; extern unsigned char fatbuf[];

//buffer for directory sectors //buffer for FAT sectors

struct FileDesc { //FileDescriptor structure unsigned char iob[BYTE_PER_SEC]; //file i/o buffer unsigned char FileName[8]; //file name unsigned char FileExt[3]; //file extension unsigned char FileDirOffset; //dir entry offset in FileDirSector/32 unsigned long FileCurrentSector; //number of sector with last data read/written unsigned long File1stClusterSector; //1st sector of current cluster used unsigned long FileDirSector; //dir sector holding this fileentry unsigned long FileSize; unsigned long FilePosition; //file byte position unsigned char FileFlag; //open or closed unsigned char FileAttr; //file attribute. also used for directory functions unsigned long FileClusterCount; //this is NOT uint ! #ifdef USE_FAT32 unsigned long FileFirstCluster; //needed for UpdateFileEntry() ! unsigned long FileCurrentCluster; //number of cluster in use

#else //#ifdef USE_FAT32 unsigned int FileFirstCluster; unsigned int FileCurrentCluster; #endif //#ifdef USE_FAT32 }; extern struct FileDesc FileDescriptors[]; #ifdef USE_FAT32 extern unsigned long FirstDirCluster; #else extern unsigned int FirstDirCluster; #endif //extern unsigned long FATHits; // count FAT write cycles. you don't really need this ;) extern unsigned long FATFirstSector; extern unsigned long FATCurrentSector; extern unsigned char FATtype; extern unsigned char FATStatus; // only for FAT write buffering extern extern extern #endif unsigned long FirstRootSector; unsigned long FirstDataSector; unsigned long RootDirSectors; //FAT_H

fatfile.h
#ifndef __FATFILE__ #define __FATFILE__ #include "dir.h" #include "dos.h" #include "drivefree.h" #include "fat.h" #include "media.h" #include "mmc_spi.h" #include "printf.h" #include "serial.h" #endif

find_x.h
#ifndef __FINDX_H #define __FINDX_H #define _MAX_NAME 255 // Max. length // // // // // // of long filenames + 1. This should be 256, but i dont want to use an unsigned int. Maybe 128 or 64 Bytes are also enough for a microcontroller DOS. Change it here if you have problems with free RAM.

struct FindFile { unsigned char ff_attr;

readonly flags are ignored unsigned long ff_fsize; // filesize of the file ( not directory ! ) char ff_name[13]; // 8.3 DOS filename with '.' in it and \0 at the end for fopen() #ifdef USE_FINDLONG char ff_longname[_MAX_NAME]; // The very long filename. #endif #ifdef USE_FAT32 unsigned long newposition; // position of this file entry in current directory (1 means first file) unsigned long lastposition; // position of last file entry found in current directory (1 means first file) #else unsigned int newposition; // position of this file entry in current directory (1 means first file) unsigned int lastposition; // position of last file entry found in current directory (1 means first file) // does also count ".", ".." entrys ! // does not count long filename entrys and volume id #endif }; extern struct FindFile ffblk; extern unsigned char Findfirst(void); extern unsigned char Findnext(void); extern unsigned char FindInOneDirectorySector(unsigned long sector); #ifdef USE_FAT32 extern unsigned char FindInSubDir(unsigned long startcluster); #else extern unsigned char FindInSubDir(unsigned int startcluster); #endif extern unsigned char FindInRootDir(void); #endif //__FINDX_H

// file attributes like file, dir // long name ,hidden,system and

media.h
#ifndef __FLASH_MEDIA_H #define __FLASH_MEDIA_H #define MMC_CARD_SPI //SD_CARD_SPI too ! #endif //__FLASH_MEDIA_H

mmc_spi.h
#ifndef __MMC_CARD_SPI_H #define __MMC_CARD_SPI_H //#define STANDARD_SPI_READ // No optimizations #define FAST_SPI_READ // Optimizations //#define STANDARD_SPI_WRITE // No optimizations #define FAST_SPI_WRITE // Optimizations #define SPI_REGISTER #define SPI_WRITE(a) #define SPI_WAIT() SPDR //makes it easier to switch to another hardware { SPI_REGISTER=(a); while(! ( SPSR & (1<<SPIF) ));} { while(! ( SPSR & (1<<SPIF) ) ); }

//#define MMC_DEBUG //activate debug output via printf() #if defined (__AVR_ATmega644__) #define SPCR SPCR0 #define SPIE SPIE0 #define SPE SPE0 #define DORD DORD0 #define MSTR MSTR0 #define CPOL CPOL0 #define CPHA CPHA0 #define SPR1 SPR01 #define SPR0 SPR00 #define #define #define #define SPSR SPIF WCOL SPI2X SPSR0 SPIF0 WCOL0 SPI2X0 SPDR0

#define SPDR #endif

#if defined (__AVR_ATmega32__) || defined (__AVR_ATmega323__) || defined (__AVR_ATmega644__) #define MMC_CS_PIN #define MMC_CS_PORT #define MMC_CS_DDR #define #define #define #define #define PORTB DDRB 4 //Pin number for MMC_CS //Port where MMC_CS is located //Port direction register where MMC_CS is located

MMC_SCK_BIT 7 MMC_SCK_PORT PORTB MMC_SCK_DDR DDRB MMC_MISO_BIT 6 MMC_MISO_PORT PORTB

#define MMC_MISO_DDR

DDRB

#define MMC_MOSI_BIT 5 #define MMC_MOSI_PORT PORTB #define MMC_MOSI_DDR DDRB #elif defined (__AVR_ATmega128__) || defined (__AVR_ATmega64__) #define MMC_CS_PIN #define MMC_CS_PORT #define MMC_CS_DDR #define MMC_SCK_BIT #define MMC_SCK_PORT #define MMC_SCK_DDR PORTB DDRB 1 PORTB DDRB 0 //Pin number for MMC_CS //Port where MMC_CS is located //Port direction register where MMC_CS is located

#define MMC_MOSI_BIT 2 #define MMC_MOSI_PORT PORTB #define MMC_MOSI_DDR DDRB #define MMC_MISO_BIT 3 #define MMC_MISO_PORT PORTB #define MMC_MISO_DDR DDRB #define #define #define #define #else # error #endif MMC_CARD_DET MMC_CARD_PORT MMC_CARD_DDR MMC_CARD_INP 4 PORTB DDRB PINB

"processor type not defined in mmc_spi.h" sbi(MMC_CS_PORT,MMC_CS_PIN); cbi(MMC_CS_PORT,MMC_CS_PIN);

#define MMC_CS_ON() #define MMC_CS_OFF()

// MMC/SD commands #define MMC_RESET 0x40 + 0 #define MMC_INIT 0x40 + 1 #define MMC_READ_CSD 0x40 #define MMC_READ_CID 0x40 #define MMC_STOP_TRANSMISSION 0x40 #define MMC_SEND_STATUS 0x40 #define MMC_SET_BLOCKLEN 0x40 #define MMC_READ_BLOCK 0x40 #define MMC_READ_MULTI_BLOCK 0x40 #define MMC_WRITE_BLOCK 0x40 #define MMC_WRITE_MULTI_BLOCK 0x40

+ + + + + + + + +

9 10 12 13 16 17 18 24 25

//prototypes extern unsigned char MMCReadSector(unsigned long sector, unsigned char *buf); extern unsigned char MMCWriteSector(unsigned long sector, unsigned char *buf); extern unsigned char MMCIdentify(void); extern void MMC_IO_Init(void); #define ReadSector(a,b) #define WriteSector(a,b) #define IdentifyMedia() #endif MMCReadSector((a),(b)) MMCWriteSector((a),(b)) MMCIdentify()

printf.h
#ifndef __PRINTF_H #define __PRINTF_H #include <avr/pgmspace.h> extern void myputchar(unsigned char c); extern void _puts_P(char const *txt); #define puts(text) _puts_P(PSTR(text))

extern void _printf_P (char const *fmt0, ...); #define printf(format, args...) _printf_P(PSTR(format) , ## args) #endif

serial.h
#ifndef ___SERIAL_H #define ___SERIAL_H #include <avr/pgmspace.h> #ifndef UART_BAUD_RATE //#define UART_BAUD_RATE #define UART_BAUD_RATE //#define UART_BAUD_RATE //#define UART_BAUD_RATE //#define UART_BAUD_RATE #endif 4800 9600 19200 38400 115200

#define UART_BAUD_SELECT (F_CPU/(UART_BAUD_RATE*16l)-1) extern extern extern extern extern extern void ser_init(void); void ser_putc(unsigned char c); void ser_puts(unsigned char *s); int ser_gets(unsigned char *s, unsigned char len); unsigned char ser_getc(void); void ser_puthex(unsigned char by);

extern volatile unsigned char rcnt, rpos; extern volatile unsigned char busy; extern void _serputs_P(char const *txt); #define serputs_P(text) _serputs_P(PSTR(text)) extern extern extern extern void char void void init_uart1 (void); uart1_ReceiveByte (void); uart1_SendByte (char data); uart1_SendString (char *str);

// Anpassungen an die vielen bescheuerten Namens nderungen die ATMEL // quasi von ATMega zu ATMega in den Datenbl ttern vornimmt. // Es k nnte soo einfach sein. #if defined (__AVR_ATmega32__) || defined (__AVR_ATmega323__) #define TX0_BIT 1 #define TX0_PORT PORTD #define TX0_DDR DDRD #define SIG_UART0_RECEIVE #define SIG_UART0_TRANSMIT #define UART0_RECEIVE_REGISTER #define UART0_TRANSMIT_REGISTER #define ENABLE_UART0_TRANSMIT_INT nicht TXCIE ? #define DISABLE_UART0_TRANSMIT_INT aber habe vergessen #define ENABLE_UART0_RECEIVE_INT #define DISABLE_UART0_RECEIVE_INT #define UART0_BAUD_REGISTER_HIGH #define UART0_BAUD_REGISTER_LOW #define UART0_CONFIGURE1 (1<<RXEN) | (1<<TXEN) #define UART0_CONFIGURE2 (1<<UCSZ0) //8 Bit,1 Stop, no parity #elif defined (__AVR_ATmega161__) #define TX0_BIT 1 #define TX0_PORT PORTD #define TX0_DDR DDRD #define #define #define #define #define #define #define #define #define #define statt 0 ? #define (1<<RXEN) #define no parity SIG_UART0_RECEIVE SIG_UART0_TRANSMIT UART0_RECEIVE_REGISTER UART0_TRANSMIT_REGISTER ENABLE_UART0_TRANSMIT_INT DISABLE_UART0_TRANSMIT_INT ENABLE_UART0_RECEIVE_INT DISABLE_UART0_RECEIVE_INT UART0_BAUD_REGISTER_HIGH UART0_BAUD_REGISTER_LOW UART0_CONFIGURE1 | (1<<TXEN) UART0_CONFIGURE2 SIG_UART0_RECV SIG_UART0_TRANS UDR0 UDR0 sbi(UCSR0B,TXEN) cbi(UCSR0B,TXEN); sbi(UCSR0B,RXCIE) cbi(UCSR0B,RXCIE) UBRRH UBRR0 // Ich glaub es nicht. Wieso kein L UCSR0B= (1<<RXCIE) | (1<<TXCIE) | NOP() //Ho, ho, hoo. 8 Bit,1 Stop, SIG_UART_RECV SIG_UART_TRANS UDR UDR sbi(UCSRB,TXEN) //Wieso nehme ich hier cbi(UCSRB,TXEN) //Es gibt einen Grund,

sbi(UCSRB,RXCIE) cbi(UCSRB,RXCIE) UBRRH UBRRL UCSRB= (1<<RXCIE) | (1<<TXCIE) | UCSRC= (1<<URSEL) | (1<<UCSZ1) |

#elif defined (__AVR_ATmega168__) || defined (__AVR_ATmega48__) || defined (__AVR_ATmega88__) || defined (__AVR_ATmega644__) #define TX0_BIT 1 #define TX0_PORT PORTD #define TX0_DDR DDRD #define SIG_UART0_RECEIVE #define SIG_UART0_TRANSMIT #define UART0_RECEIVE_REGISTER nicht. Ich schmei mich weg !!! #define UART0_TRANSMIT_REGISTER #define ENABLE_UART0_TRANSMIT_INT SIG_USART_RECV SIG_USART_TRANS UDR0 // UDR hat ne Null hinten, aber USART UDR0 sbi(UCSR0B,TXEN0);

#define DISABLE_UART0_TRANSMIT_INT cbi(UCSR0B,TXEN0); #define ENABLE_UART0_RECEIVE_INT sbi(UCSR0B,RXCIE0) #define DISABLE_UART0_RECEIVE_INT cbi(UCSR0B,RXCIE0) #define UART0_BAUD_REGISTER_HIGH UBRR0H #define UART0_BAUD_REGISTER_LOW UBRR0L #define UART0_CONFIGURE1 UCSR0B= (1<<RXCIE0) | (1<<TXCIE0) | (1<<RXEN0) | (1<<TXEN0) // Was sollen jetzt die 0en hinter den Bits ? #define UART0_CONFIGURE2 UCSR0C= (1<<UCSZ01) | (1<<UCSZ00) //8 Bit,1 Stop, no parity #elif defined (__AVR_ATmega8__) #define TX0_BIT 1 #define TX0_PORT PORTD #define TX0_DDR DDRD #define SIG_UART0_RECEIVE #define SIG_UART0_TRANSMIT #define UART0_RECEIVE_REGISTER #define UART0_TRANSMIT_REGISTER #define ENABLE_UART0_TRANSMIT_INT #define DISABLE_UART0_TRANSMIT_INT #define ENABLE_UART0_RECEIVE_INT #define DISABLE_UART0_RECEIVE_INT #define UART0_BAUD_REGISTER_HIGH #define UART0_BAUD_REGISTER_LOW #define UART0_CONFIGURE1 (1<<RXEN) | (1<<TXEN) #define UART0_CONFIGURE2 Bit,1 Stop, no parity SIG_UART_RECV SIG_UART_TRANS UDR UDR sbi(UCSRB,TXEN); cbi(UCSRB,TXEN); sbi(UCSRB,RXCIE) cbi(UCSRB,RXCIE) UBRRH UBRRL UCSRB= (1<<RXCIE) | (1<<TXCIE) | UCSRC= (1<<UCSZ1) | (1<<UCSZ0) //8

#elif defined (__AVR_ATmega128__) || defined (__AVR_ATmega64__) #define TX0_BIT 1 #define TX0_PORT PORTE #define TX0_DDR DDRE #define #define #define #define #define #define #define #define SIG_UART0_RECEIVE SIG_UART0_TRANSMIT UART0_RECEIVE_REGISTER UART0_TRANSMIT_REGISTER ENABLE_UART0_TRANSMIT_INT DISABLE_UART0_TRANSMIT_INT ENABLE_UART0_RECEIVE_INT DISABLE_UART0_RECEIVE_INT SIG_UART0_RECV SIG_UART0_TRANS UDR0 UDR0 sbi(UCSR0B,TXEN) cbi(UCSR0B,TXEN); sbi(UCSR0B,RXCIE) cbi(UCSR0B,RXCIE) UBRR0H UBRR0L UCSR0B= (1<<RXCIE) | (1<<TXCIE) | UCSR0C= (1<<UCSZ1) | (1<<UCSZ0) //8

#define UART0_BAUD_REGISTER_HIGH #define UART0_BAUD_REGISTER_LOW #define UART0_CONFIGURE1 (1<<RXEN) | (1<<TXEN) #define UART0_CONFIGURE2 Bit,1 Stop, no parity

#else # error "processor type not defined in serial.h" #endif #endif

Vous aimerez peut-être aussi