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 ?
Thanks so much
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