Em mới bắt đầu học lập trình PIC nên còn non lắm. Đang làm đề tài giao tiếp thẻ nhớ MMC với PIC, mà khi lập trình bằng MikroC dịch ra thì mô phỏng toàn bị không nhận được thẻ. Vì vậy viết lên đây mong mọi người chỉ giúp lỗi sai
Trong code thì, nó luôn chạy ở chế độ Mmc_Init() = 1
Code:
// Ket noi voi LCD module sbit LCD_RS at RB2_bit; sbit LCD_EN at RB3_bit; sbit LCD_D4 at RB4_bit; sbit LCD_D5 at RB5_bit; sbit LCD_D6 at RB6_bit; sbit LCD_D7 at RB7_bit; sbit LCD_RS_Direction at TRISB2_bit; sbit LCD_EN_Direction at TRISB3_bit; sbit LCD_D4_Direction at TRISB4_bit; sbit LCD_D5_Direction at TRISB5_bit; sbit LCD_D6_Direction at TRISB6_bit; sbit LCD_D7_Direction at TRISB7_bit; // Ket noi MMC sbit Mmc_Chip_Select at RC2_bit; sbit Mmc_Chip_Select_Direction at TRISC2_bit; // Text hien thi len LCD char txt1[] = "MMC MODULE"; char txt2[] = "TRINH VAN LUONG"; char txt3[] = "BAI MO PHONG"; char txt4[] = "NGAY: 19/9/2014"; char txt5[] = "WRITING..."; char txt6[] = "LPG:"; char err_txt[20] = "Not FAT16 format"; // Variable char cont; char filename[14] = "FILE_00x.TXT"; char first_cont[] = "KET QUA DO: \n\r"; unsigned int adc_rs; long tong; char tmp[8]; float kq, ch; unsigned int gtri; // Ham con char New_Text(char *txt) { int i, j=0; char tem[12]; for(i=0;i<=sizeof(txt);i++) { tem[i]=txt[i]; j++; } tem[j] = 0x0D; // Them ky tu xuong dong j++; tem[j] = 0x0A; // Them ky tu tao dong moi j++; tem[j] = '\0'; return tem; } void Get_Tong() { tong = (long)adc_rs*5000; tong = tong/1023; kq = tong/1000; ch = (tong/100)%10; kq += ch*0.1; ch = (tong/10)%10; kq += ch*0.01; } void Get_ADC0() { adc_rs = ADC_Read(0); Get_tong(); gtri= 1.5*(1192*kq*kq*kq - 82.77*kq*kq + 140.1*kq + 1.215)/2 - 11; if(gtri<25.1) { PORTD.F4 = 1; // Bao den Blue; Delay_ms(50); PORTC.F0 = 0; } if((gtri>=25.1) && (gtri<50.1)) { PORTD.F5 = 1; // Bao den Green; Delay_ms(50); PORTC.F0 = 0; } if((gtri>=50.1) && (gtri<100.1)) { PORTD.F6 = 1; // Bao den Vang Delay_ms(50); PORTC.F0 = 0; } if(gtri>100.1) { PORTD.F7 = 1; // Bao den Do Delay_ms(50); PORTC.F0 = 1; // Ru' coi } } void M_Create_File(char cont) { // Ham tao file moi int i; filename[7] = '1'; // Ten file: FILE_00A.txt //A: i = Mmc_Fat_Init(); // Khoi dong FAT Delay_ms(20); if (i == 0) { SPI1_Init_Advanced(_SPI_MASTER_OSC_DIV4, _SPI_DATA_SAMPLE_MIDDLE, _SPI_CLK_IDLE_LOW, _SPI_LOW_2_HIGH); // Set high speed if(Mmc_Fat_Exists(&filename) == 0) { Lcd_Cmd(_LCD_CLEAR); Lcd_Out(1,1, "Creat New File"); Mmc_Fat_Set_File_Date(2014, 8, 30, 9, 0, 0); Mmc_Fat_Assign(&filename, 0x80); // Tim 1 file da ton tai hoac tao mot file moi Mmc_Fat_Rewrite(); Mmc_Fat_Write(first_cont, sizeof(first_cont)); Delay_ms(50); } else { Lcd_Cmd(_LCD_CLEAR); Lcd_Out(1,1, "Saving..."); Mmc_Fat_Set_File_Date(2014, 8, 19, 9, 0, 0); // Set file date & time info Mmc_Fat_Assign(&filename, 0); // Kiem tra FILE_00B.txt Mmc_Fat_Append(); // Xac dinh che do them noi dung // Do dai data Mmc_Fat_Write(cont, sizeof(cont)); // Write data to assigned file Delay_ms(50); } } if(i == 1) { Lcd_Cmd(_LCD_CLEAR); Lcd_Out(1,1,err_txt); Lcd_Out(2,1,"Formatting..."); Delay_ms(50); Mmc_Fat_QuickFormat("MMCCard"); i = Mmc_Fat_Init(); //goto A; } else { Lcd_Out(1,1, " No MMC Card! "); } } // Ham chinh void main() { int mod; // ADCON0 = 0b00000011; // Set Chanel 0, ADC enable ADCON1 = 0b00001001; ADCON2 = 0xA6; // Set tan so lam viec, thoi gian thu thap RB0_bit = 0; RB1_bit = 0; RC7_bit = 0; TRISB1_bit = 0; //clk TRISB0_bit = 0; //sdo TRISC7_bit = 1; //sdi Lcd_Init(); Lcd_Cmd(_LCD_CLEAR); // Clear display Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off Lcd_Out(1,4,txt1); Lcd_Out(2,1,txt2); Delay_ms(2000); Lcd_Cmd(_LCD_CLEAR); Lcd_Out(1,1,txt3); Lcd_Out(2,1,txt4); Delay_ms(2000); Lcd_Cmd(_LCD_CLEAR); SPI1_Init_Advanced(_SPI_MASTER_OSC_DIV64, _SPI_DATA_SAMPLE_MIDDLE, _SPI_CLK_IDLE_LOW, _SPI_LOW_2_HIGH); // Set low speed mod = Mmc_Init(); if(mod == 0) { //C: Lcd_Cmd(_LCD_CLEAR); Lcd_Out(1,1, "Card detected"); Delay_ms(500); while(mod == 0) { Get_ADC0(); Lcd_Cmd(_LCD_CLEAR); Lcd_Out(2,1, txt6); IntToStr(gtri, tmp); Lcd_Out(2,6, tmp); M_Create_File(New_Text(&tmp)); Delay_ms(1000); } } if(mod == 1) { while(mod) { Get_ADC0(); Lcd_Cmd(_LCD_CLEAR); Lcd_Out(1,1, "No Card detected"); Lcd_Out(2,1, txt6); IntToStr(gtri, tmp); Lcd_Out(2,6, tmp); Delay_ms(1000); if (Mmc_Init()==0){ mod = 0;} } //goto C; } }
Comment