check my codes for PIC18F4520

Status
Not open for further replies.

InNeedOfHelp

Full Member level 3
Joined
Dec 5, 2012
Messages
169
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,298
Activity points
2,205
Can anyone help me check whether this code is correct anot ?
Code:
//Define .hex file//
#include <p18f4520.h>
#include <usart.h>
#include <delays.h>


//Define Configuration bits//
#pragma config OSC = INTIO67 //Set to use internal oscillator // No external oscillator
#pragma config WDT = OFF    //Turn off watchdog timer
#pragma config LVP = OFF    //Turn off low voltage program
#pragma config DEBUG = OFF  //Turn off debug mode




//Define Function//
void Call_Init_PIC (void);
void Call_Init_USART(void);
void Call_Init_I2C (void);
unsigned char  Get_Temp_Result (void);
void Start_I2C (void);
void Send_To_Slave(unsigned char code);
void Wait_For_ACK (void);
void Restart_I2C(void);
void Switch_To_Read_Mode (void);
unsigned char Load_From_Slave(void);
void Send_NACK(void);
void Stop_I2C(void);
void Display_In_BCD (unsigned char this_number);




//Define Global variable//
unsigned char data;

//Start of Main Function//
void main()

{
	Call_Init_PIC (); // Call to initialize PIC microcontroller
	Call_Init_USART(); // Call to initialize USART
	Call_Init_I2C ();  // Call to initialize I2C

	while (1)			// run forever; infinite loop
	{  
               
		data= Get_Temp_Result (); //Get Result from TC74
		Display_In_BCD (data); //Display Temperature result in BCD (then convert to ASCII code)
		putcUSART(32); //space 
		putcUSART(32); //space
		Delay10KTCYx(100); // one second delay
	}

}

//End of Main Function//

//////////Supporting Functions//////////

//Some supporting functions for I2C are written for other formats; Write Byte Format, Read Byte Format


void Call_Init_PIC (void)  //initialize PIC microcontroller

{
	OSCCON = 0x62; //using 4Mhz internal oscillator / No external oscillator
	ADCON1 =0x0f;//; all port as digital I/O pins
}

void Call_Init_I2C (void)
{

	TRISCbits.TRISC3=1; //Set RC3 and RC4 as input
	TRISCbits.TRISC4=1;
	SSPCON1 = 0b00101000; // No collision/ No overflow/ SDA and SCL as serial port/ Clock stretch/ Enable I2C master mode
	SSPSTATbits.SMP = 1; // Disable slew rate control for Standard Speed mode.
	SSPADD = 0x09; // Set Baud rate as 100kHZ

}

void Start_I2C (void)
{
	SSPCON2bits.SEN = 1; // Enable start condition 
	while ( SSPCON2bits.SEN)Nop(); // Wait for SEN to go LOW.
}

void Send_To_Slave(unsigned char code)
{
	PIR1bits.SSPIF=0; // clear SSPIF flag
	SSPBUF = code;   // Send TC74 ADDRESS; default is 1001101 + (0 write or 1 read)
}

void Wait_For_ACK (void)
{
	while (SSPCON2bits.ACKSTAT); // Wait for ACKSTAT to go LOW. 
	while(!PIR1bits.SSPIF)Nop(); // Wait for SSPIF flag to go HIGH
}

void Restart_I2C(void)
{
	SSPCON2bits.RSEN = 1;  // Repeated Start Condition Enable bit
	while (SSPCON2bits.RSEN)Nop();  //Wait for RSEN to go LOW. 
}

void Switch_To_Read_Mode (void)
{
	SSPCON2bits.RCEN = 1;  // Enable Receive mode
	while ( SSPCON2bits.RCEN)Nop();  // Wait for RSEN to go LOW. 
}

unsigned char Load_From_Slave(void)
{
	unsigned char temp;
	temp = SSPBUF; // Load to "temp" variable  
	return temp;
}

void Send_NACK(void)
{
	SSPCON2bits.ACKDT = 1;// Send NOT-ACK. 
	SSPCON2bits.ACKEN = 1;
	while ( SSPCON2bits.ACKEN) Nop(); // Wait for ACKEN to go LOW. 
}

void Stop_I2C(void)
{
	SSPCON2bits.PEN = 1; // Initiate STOP condition 
	while( SSPCON2bits.PEN) Nop(); //  Wait for PEN to go LOW. 
}

unsigned char  Get_Temp_Result (void)
{
	unsigned char temp;
	//Write According to Receive Byte Format
	Start_I2C ();  //Start Condition
	Send_To_Slave(155)  ;  //Send to slave 7bit Address (1001 101) + RD (1)
	Wait_For_ACK(); //Wait for ACK from slave
	Switch_To_Read_Mode(); //Switch to Read mode in order to receive from Slave
	temp = Load_From_Slave(); //Get data from Slave
	Send_NACK(); //Send NACK 
	Stop_I2C(); //Stop Condition 
	return temp;
}


void Display_In_BCD (unsigned char this_number)
{
	unsigned char First_Deci; //First Decimal //Local variables
	unsigned char Second_Deci; //Second Decimal
	unsigned char Third_Deci; //Third Decimal

	//the limit of TC74 sensor: +127 to -65 degree

	if (this_number > 127 ) //if more than 127 then go to minus range
		
	{
		this_number = 256- this_number;
		putcUSART(45); //put minus sign
	}
		

	First_Deci = this_number/100;
	Second_Deci = (this_number /10)%10;
	Third_Deci = (this_number/1)%10	;
			
	if (First_Deci != 0){putcUSART(48+First_Deci);}  //Show first digit
		
		Delay10KTCYx(10); 
		if (Second_Deci != 0 || First_Deci != 0){putcUSART(48+Second_Deci);} //Show second digit
		Delay10KTCYx(10); 
		putcUSART(48+Third_Deci); //Show third digit
		Delay10KTCYx(10); 
		putcUSART(39); //Show "'"
		Delay10KTCYx(10); 
		putcUSART(67); //Show "C"

}

void Call_Init_USART()
{
		OpenUSART( USART_TX_INT_OFF &
		USART_RX_INT_OFF &
		USART_ASYNCH_MODE &
		USART_EIGHT_BIT &
		USART_CONT_RX &
		USART_BRGH_HIGH,	
		25); //Bit per second:9600 / Data bit: 8 / Partiy bit : None / Stop bit : 1 / Flow control : None
}

Thanks so much
 

why you are using USARAT and I2C?what is the problem actually?
 

Im doing a program to display temperature from TC74 to the LCD ..
 

Ok What is the problem occurring during the execution?
 

nothing is displaying on the LCD,
Codes is built successfully
 

i think you are not initialized the LCD in program you have to define that too

Code:
sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D4 at RB0_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D7 at RB3_bit;

sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;

void main()
{
 Lcd_Init();                        // Initialize LCD

 Lcd_Out(1,6,"hello");                 // Write text in first row

}

you also need to send command for lcd what you want to print
 

Which Compiler are you using? C18 or Hi-Tech C?

Edit: You are using C18. You have to send 0x9A first to I2C. you are sending 0x9B which is d155.
 

so is send d154 rigght ?

- - - Updated - - -

change to d154 at here right ?

Code:
Start_I2C ();  //Start Condition
	Send_To_Slave(154)  ;  //Send to slave 7bit Address (1001 101) + RD (1)
	Wait_For_ACK(); //Wait for ACK from slave
	Switch_To_Read_Mode(); //Switch to Read mode in order to receive from Slave
	temp = Load_From_Slave(); //Get data from Slave
	Send_NACK(); //Send NACK

still cannot display after changing it
 

Code:
//Define .hex file//
#include <p18f4520.h>
#include <usart.h>
#include <delays.h>
#include <i2c.h>


//Define Configuration bits//
#pragma config OSC = INTIO67 //Set to use internal oscillator // No external oscillator
#pragma config WDT = OFF    //Turn off watchdog timer
#pragma config LVP = OFF    //Turn off low voltage program
#pragma config DEBUG = OFF  //Turn off debug mode




//Define Function//
void Call_Init_PIC (void);
void Call_Init_USART(void);
void Call_Init_I2C (void);
unsigned char  Get_Temp_Result (void);
void Start_I2C (void);
void Send_To_Slave(unsigned char code);
void Wait_For_ACK (void);
void Restart_I2C(void);
void Switch_To_Read_Mode (void);
unsigned char Load_From_Slave(void);
void Send_NACK(void);
void Stop_I2C(void);
void Display_In_BCD (unsigned char this_number);




//Define Global variable//
unsigned char data;

//Start of Main Function//
void main()

{
	Call_Init_PIC (); // Call to initialize PIC microcontroller
	Call_Init_USART(); // Call to initialize USART
	Call_Init_I2C ();  // Call to initialize I2C

	while (1)			// run forever; infinite loop
	{  
               
		data= Get_Temp_Result (); //Get Result from TC74
		Display_In_BCD (data); //Display Temperature result in BCD (then convert to ASCII code)
		putcUSART(32); //space 
		putcUSART(32); //space
		Delay10KTCYx(100); // one second delay
	}

}

//End of Main Function//

//////////Supporting Functions//////////

//Some supporting functions for I2C are written for other formats; Write Byte Format, Read Byte Format


void Call_Init_PIC (void)  //initialize PIC microcontroller

{
	OSCCON = 0x62; //using 4Mhz internal oscillator / No external oscillator
	ADCON1 =0x0f;//; all port as digital I/O pins
}

void Call_Init_I2C (void)
{

	TRISCbits.TRISC3=1; //Set RC3 and RC4 as input
	TRISCbits.TRISC4=1;
	SSPCON1 = 0x28; // No collision/ No overflow/ SDA and SCL as serial port/ Clock stretch/ Enable I2C master mode
	SSPSTAT=0x80; // Disable slew rate control for Standard Speed mode.
	SSPADD = 119; // Set Baud rate as 100kHZ

}

void Start_I2C (void)
{
	SSPCON2bits.SEN = 1; // Enable start condition 
	while ( SSPCON2bits.SEN)Nop(); // Wait for SEN to go LOW.
}

void Send_To_Slave(unsigned char code)
{
	PIR1bits.SSPIF=0; // clear SSPIF flag
	SSPBUF = 0b10011010;   // Send TC74 ADDRESS; default is 1001101 + (0 write or 1 read)
}

void Wait_For_ACK (void)
{
	while (SSPCON2bits.ACKSTAT); // Wait for ACKSTAT to go LOW. 
	while(!PIR1bits.SSPIF)Nop(); // Wait for SSPIF flag to go HIGH.
}

void Restart_I2C(void)
{
	SSPCON2bits.RSEN = 1;  // Repeated Start Condition Enable bit
	while (SSPCON2bits.RSEN)Nop();  //Wait for RSEN to go LOW. 
}

void Switch_To_Read_Mode (void)
{
	SSPCON2bits.RCEN = 1;  // Enable Receive mode
	while ( SSPCON2bits.RCEN)Nop();  // Wait for RSEN to go LOW. 
}

unsigned char Load_From_Slave(void)
{
	unsigned char temp;
	temp = SSPBUF; // Load to "temp" variable  
	return temp;
}

void Send_NACK(void)
{
	SSPCON2bits.ACKDT = 1;// Send NOT-ACK. 
	SSPCON2bits.ACKEN = 1;
	while ( SSPCON2bits.ACKEN) Nop(); // Wait for ACKEN to go LOW. 
}

void Stop_I2C(void)
{
	SSPCON2bits.PEN = 1; // Initiate STOP condition 
	while( SSPCON2bits.PEN) Nop(); //  Wait for PEN to go LOW. 
}

unsigned char  Get_Temp_Result (void)
{
	unsigned char temp;
	//Write According to Receive Byte Format
	Start_I2C ();  //Start Condition
	Send_To_Slave(0b10011010)  ;  //Send to slave 7bit Address (1001 101) + RD (1)
	Wait_For_ACK(); //Wait for ACK from slave
	Switch_To_Read_Mode(); //Switch to Read mode in order to receive from Slave
	temp = Load_From_Slave(); //Get data from Slave
	Send_NACK(); //Send NACK 
	Stop_I2C(); //Stop Condition 
	return temp;
}


void Display_In_BCD (unsigned char this_number)
{
	unsigned char First_Deci; //First Decimal //Local variables
	unsigned char Second_Deci; //Second Decimal
	unsigned char Third_Deci; //Third Decimal

	//the limit of TC74 sensor: +127 to -65 degree

	if (this_number > 127 ) //if more than 127 then go to minus range
		
	{
		this_number = 256- this_number;
		putcUSART(45); //put minus sign
	}
		

	First_Deci = this_number/100;
	Second_Deci = (this_number /10)%10;
	Third_Deci = (this_number/1)%10	;
			
	if (First_Deci != 0){putcUSART(48+First_Deci);}  //Show first digit
		
		Delay10KTCYx(10); 
		if (Second_Deci != 0 || First_Deci != 0){putcUSART(48+Second_Deci);} //Show second digit
		Delay10KTCYx(10); 
		putcUSART(48+Third_Deci); //Show third digit
		Delay10KTCYx(10); 
		putcUSART(39); //Show "'"
		Delay10KTCYx(10); 
		putcUSART(67); //Show "C"

}

void Call_Init_USART()
{
		OpenUSART( USART_TX_INT_OFF & 
		USART_RX_INT_OFF &
		USART_ASYNCH_MODE &
		USART_EIGHT_BIT &
		USART_CONT_RX &
		USART_BRGH_HIGH,	
		25); //Bit per second:9600 / Data bit: 8 / Partiy bit : None / Stop bit : 1 / Flow control : None
}

Hi Jayanth , it is still not working. any more suggestion ?
i have followed the link already,
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…