PIC18F4520 & MikroC & SD card & UART

Status
Not open for further replies.

Linspire

Full Member level 5
Joined
Sep 1, 2011
Messages
303
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,298
Location
M'sia
Activity points
3,279
Guys
I have done simple testing for RS-232 and SD card testing.
They are working properly now.
However when I combined both of them function, it doesn't display correct display and save wrong info input from RS-232.
Coding will posted as below:
Code:
  ADCON1 |= 0x0F;                  // Configure AN pins as digital
  CMCON  |= 7;
  TRISB = 0;                    // Turn off comparators
  PortB = 0xFF;

  UART1_Init(9600);
  Delay_ms(1000);
  Lcd_Init();
  // Initialize SPI1 module
  SPI1_Init_Advanced(_SPI_MASTER_OSC_DIV64, _SPI_DATA_SAMPLE_MIDDLE,_SPI_CLK_IDLE_LOW, _SPI_LOW_2_HIGH);
  while (Mmc_Init());
  Lcd_Out(1,1,"SPI reintialized");
  Delay_ms(1000);
  Delay_ms(1000);
  // use fat16 quick format instead of init routine if a formatting is needed
  while(1) {
           // use fat16 quick format instead of init routine if a formatting is needed
           if (Mmc_Fat_Init() == 0) {
           Lcd_Cmd(_LCD_CLEAR);
           Lcd_Out(1,1,"MMC init OK");
           // reinitialize spi at higher speed
           SPI1_Init_Advanced(_SPI_MASTER_OSC_DIV4, _SPI_DATA_SAMPLE_MIDDLE, _SPI_CLK_IDLE_LOW, _SPI_LOW_2_HIGH);
           if (UART1_Data_Ready()){              //<------ Check if a character has been received before reading
              uart_rd = UART1_Read();     // read the received data,
              UART1_Write(uart_rd);
              data2[number] = uart_rd;
              number = number +1;
              if (number == 12) // temp storing  string
              {
                 Lcd_Cmd(_LCD_CLEAR);
                 Lcd_cmd(_LCD_RETURN_HOME);
                 Lcd_out(1,1,data2);
                 Mmc_Fat_Set_File_Date(2010, 4, 19, 9, 0, 0); // Set file date & time info
                 Mmc_Fat_Assign(&filename, 0xA0);          // Find existing file or create a new one
                 Mmc_Fat_Rewrite;
                 Mmc_Fat_Write(data2,number);
                 Mmc_Fat_Write("\r\n",2);
                 Delay_ms(2000);
                 Lcd_Cmd(_LCD_CLEAR);
                 Lcd_Out(1,1,"Test End");
                 break;
               }
           }

           }else {
                 Lcd_Cmd(_LCD_CLEAR);
                 Lcd_Out(1,1,"MMC init FAILED"); // Note: Mmc_Fat_Init tries to initialize a card more than once.
                                                //       If card is not present, initialization may last longer (depending on clock speed)
                 }
            }


Regards
Linspire
 
Last edited:

sample working code:

Code:
//********************************************************************** 
/*

*/
//********************************************************************** 

#define		LED			PORTB.F0

#define		SW_START	PORTD.F7
#define		SW_STOP		PORTD.F6
#define		SW_MODE		PORTD.F5

#define		ON			1
#define		OFF			0

#define		CR			0x0d
#define		LF			0x0a

#define		DATA_SIZE	128

unsigned	short	flg;
unsigned	short	msg1[DATA_SIZE], len1;
unsigned	short	msg2[DATA_SIZE], len2;
unsigned	short	msg3[DATA_SIZE], len3;

//********************************************************************** 

static	char	buf[32];

//********************************************************************** 

void	interrupt()
{
	while (Usart_Data_Ready()) {
		switch (flg) {
		case 0:
			msg1[len1] = Usart_Read();
			len1++;
			if (len1 == DATA_SIZE) {
				flg = 1;
			}
			break;
		case 1:
			msg2[len2] = Usart_Read();
			len2++;
			if (len2 == DATA_SIZE) {
				flg = 2;
			}
			break;
		case 2:
			msg3[len3] = Usart_Read();
			len3++;
			if (len3 == DATA_SIZE) {
				flg = 0;
			}
			break;
		}
	}
}//~

//********************************************************************** 

void	Usart_Write_Str(char *str) 
{
	unsigned	short	i;

	i = 0;
	while (str[i] != 0x00) {
		USART_Write(str[i]);
		i++;
	}
	USART_Write(CR);
	USART_Write(LF);
}//~

//********************************************************************** 

void	SDCwriter()
{
	
	short			cnt;
	long			dataSize;
	
	flg = 0;
	len1 = 0;
	len2 = 0;
	len3 = 0;
	dataSize = 0;
	//
	Lcd_Custom_Out(1, 1, "SDC-Writer");
	for (cnt = 0; cnt < 10; cnt++) {
		LED = ON;
		Delay_ms(50);
		LED = OFF;
		Delay_ms(50);
	}

	Mmc_Fat_Assign("writer.txt", 0xA0);
	Mmc_Fat_Rewrite();
//	Mmc_Fat_Write("$START\r\n", 8);
	PIE1.RCIE = 1;
	PIR1.RCIF = 0;
	INTCON.PEIE = 1;
	INTCON.GIE = 1;
	
	while (SW_STOP == 1) {
		if (len1 == DATA_SIZE) {
			LED = ON;
			Mmc_Fat_Write(msg1, len1);
			LED = OFF;
			len1 = 0;
			//
			dataSize += DATA_SIZE;
		}
		if (len2 == DATA_SIZE) {
			LED = ON;
			Mmc_Fat_Write(msg2, len2);
			LED = OFF;
			len2 = 0;
			//
			dataSize += DATA_SIZE;
		}
		if (len3 == DATA_SIZE) {
			LED = ON;
			Mmc_Fat_Write(msg3, len3);
			LED = OFF;
			len3 = 0;
			//
			dataSize += DATA_SIZE;
		}
		
		LongToStr(dataSize, buf);
		Lcd_Custom_Out(2, 1, buf);
	}
	
	PIE1.RCIE = 0;
	PIR1.RCIF = 0;
	INTCON.PEIE = 0;
	INTCON.GIE = 0;
	//
	switch (flg) {
	case 0:
		LED = ON;
		Mmc_Fat_Write(msg1, len1);
		dataSize = dataSize + len1;
		LED = OFF;
		break;
	case 1:
		LED = ON;
		Mmc_Fat_Write(msg2, len2);
		dataSize = dataSize + len2;
		LED = OFF;
		break;
	case 2:
		LED = ON;
		Mmc_Fat_Write(msg3, len3);
		dataSize = dataSize + len3;
		LED = OFF;
		break;
	}
	//
//	Mmc_Fat_Write("$STOP\r\n", 7);
	
	LongToStr(dataSize, buf);
	Lcd_Custom_Out(2, 1, buf);
	//
	for (cnt = 0; cnt < 10; cnt++) {
		LED = ON;
		Delay_ms(50);
		LED = OFF;
		Delay_ms(50);
	}
	Lcd_Custom_Out(1, 1, "          ");
}

//********************************************************************** 

void	SDCreader()
{
	
	unsigned	long	fsize, i;
	unsigned	short	character, cnt;
	//
	Lcd_Custom_Out(1, 1, "SDC-Reader");
	for (cnt = 0; cnt < 10; cnt++) {
		LED = ON;
		Delay_ms(50);
		LED = OFF;
		Delay_ms(50);
	}
	//
	Mmc_Fat_Assign("reader.txt", 0);
	fsize = Mmc_Fat_Get_File_Size();
//	Mmc_Fat_Reset(&fsize);
	for (i = 1; i <= fsize; i++) {
		Mmc_Fat_Read(&character);
		Usart_Write(character);
	
		LongToStr(i, buf);
		Lcd_Custom_Out(2, 1, buf);
		//
		if (SW_STOP == 0)
			break;
	}
	//
	for (cnt = 0; cnt < 10; cnt++) {
		LED = ON;
		Delay_ms(50);
		LED = OFF;
		Delay_ms(50);
	}
	Lcd_Custom_Out(1, 1, "          ");
}

//********************************************************************** 

void	main()
{
	short			cnt;
	
	TRISA = 0b11111111;
	TRISB = 0b00000000;
	TRISC = 0b10010000;
	TRISD = 0b11111111;
	ADCON1.PCFG3 = 0;
	ADCON1.PCFG2 = 1;
	ADCON1.PCFG1 = 1;
	ADCON1.PCFG0 = 1;
	CMCON.CM2 = 1;
	CMCON.CM1 = 1;
	CMCON.CM0 = 1;

	Lcd_Custom_Config(&PORTB,7,6,5,4,&PORTB,1,2,3);
	Lcd_Custom_Cmd(LCD_CURSOR_OFF);
	Lcd_Custom_Cmd(LCD_CLEAR);
	for (cnt = 1; cnt <= 16; cnt++) {
		Lcd_Custom_Chr(1, cnt, 0xFF);
		LED = ON;
		Delay_ms(30);
		LED = OFF;
		Delay_ms(30);
	}
	for (cnt = 1; cnt <= 16; cnt++) {
		Lcd_Custom_Chr(2, cnt, 0xFF);
		LED = ON;
		Delay_ms(30);
		LED = OFF;
		Delay_ms(30);
	}
	Lcd_Custom_Cmd(LCD_CLEAR);
	
	Spi_Init_Advanced(MASTER_OSC_DIV64, DATA_SAMPLE_MIDDLE, CLK_IDLE_LOW, LOW_2_HIGH);
	if (Mmc_Fat_Init(&PORTC, 2)) {
		Lcd_Custom_Out(1, 1, "MMC error!");
		while (1) {
			LED = ON;
			Delay_ms(50);
			LED = OFF;
			Delay_ms(50);
		}
	}	
	Spi_Init_Advanced(MASTER_OSC_DIV16, DATA_SAMPLE_MIDDLE, CLK_IDLE_LOW, LOW_2_HIGH);
	
	Usart_Init(9600);
	Delay_ms(100);
	
	PIE1.RCIE = 0;
	PIR1.RCIF = 0;
	INTCON.PEIE = 0;
	INTCON.GIE = 0;
	//
	while (1) {
		
		while (SW_START == 1) {
			Delay_ms(10);
		}
		//
		if (SW_MODE == 1)
			SDCwriter();
		else
			SDCreader();
	}
}//~!

//**********************************************************************
 

Wow..can you explain brief explanation and show connection diagram ?


Linspire
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…