good afternoon
gentlemen
I need to migrate from 16f877 to 18F4520 18F4520 but does not have the same instructions that 16f made some changes based on the datsheet 18f but not working without going straight to the sensor response, I need to know if these changes I've made are correct if not 're correct me by favor.os points concerned the program are marked with a question.
sbit at RA0_bit Date ;
sbit at DataDir TRISA0_bit ;
message1 char [ ] = "Temp = 00.0 C " ;
message2 char [ ] = "RH = 00.0 % ;"
unsigned short TOUT = 0 , CheckSum , i ;
unsigned short T_Byte1 , T_Byte2 , RH_Byte1 , RH_Byte2 ;
StartSignal void ( ) {
DataDir = 0 / / Data port is output
Data = 0 ;
Delay_ms ( 25);
Date = 1 ;
Delay_us (30);
DataDir = 1 / / Data port is input
}
unsigned short CheckResponse ( ) {
TOUT = 0;
TMR2 = 0;
T2CON.TMR2ON = 1 / / start timer
while ( ! date && ! TOUT ) ;
if ( TOUT ) return 0;
else {
TMR2 = 0;
while ( date && ! TOUT ) ;
if ( TOUT ) return 0;
else {
T2CON.TMR2ON = 0;
return 1;
}
}
}
unsigned short ReadByte ( ) {
unsigned short a = 0 , t ;
DataDir = 1;
for (i = 0 : i 100 ) a | = 1 40us , Date is 1 / / I put 200 on the counter this correct?
}
return a ;
}
void interrupt () {
if ( PIR1.TMR2IF ) {
TOUT = 1;
T2CON.TMR2ON = 0 / / stop timer
PIR1.TMR2IF = 0 / / Clear TMR0 interrupt flag
}
}
void main ( ) {
unsigned short check ;
ANSEL = 0 / / Set AN pins to digital / how can I configure for PIC18F4520 with external clock 20mhz ?
ANSELH = 0 ; / how can I configure for PIC18F4520 ? with external clock 20mhz ?
C1ON_bit = 0 / / Disable comparators / how can I configure for PIC18F4520 with external clock 20mhz ?
C2ON_bit = 0 ; / how can I configure for PIC18F4520 with external clock 20mhz ? / /
these were my changes but not working is correct what I did ?
ADCON0 = 0x00 ; / / Configure AN pins to Digital
ADCON1 = 0b00001110 / / AN0 ENABLED
ADCON2 = 0b10000100 / / BUILD ALL CHANNELS , JUSTIFIED RIGHT UNLESS THE YEAR
ADCON0.ADON = 1 ;/ / Enables the convert
ADCON0.GO_NOT_DONE = 1 / / BEGIN CONVERSION
while ( ADCON0.GO_DONE ) ;/ / WAITING conversion finished
ADCON0.ADON = 0 ;/ / DISABLED THE CONVERTER
ADCON0.ADON = 1 ;/ / ENABLE THE CONVERTER
CCP1CON = 0 / / C1ON_bit = 0 / / Disable comparators
CCP2CON = 0 ;/ / C2ON_bit = 0;
end of my changes but it is not working , I made this change to migrate this correct ?
TRISB = 0;
TRISA = 0x01 , / / configure the input pins
TRISD = 0;
TRISB = 0;
TRISA = 0x01 ;
TRISD = 0, / / configure the input pins
INTCON.GIE = 1 / / Enable global interrupt
INTCON.PEIE = 1 / / Enable peripheral interrupt
/ / Configure Timer2 module
PIE1.TMR2IE = 1 / / Enable Timer2 interrupt
T2CON = 0 ; / / Prescaler 1:1 , and Timer2 is off INITIALLY
PIR1.TMR2IF = 0 / / Clear INT flag bit TMR
TMR2 = 0;
Lcd_Init ();
Lcd_Cmd ( _Lcd_Clear );
Lcd_Cmd ( _LCD_CURSOR_OFF );
do {
Delay_ms ( 1000) ;
StartSignal ();
CheckResponse check = ( ) ;
if ( ! check ) {
Lcd_Cmd ( _Lcd_Clear );
Lcd_Out ( 1, 1, " In response" );
Lcd_Out ( 2, 1, " from the sensor ");
}
else {
RH_Byte1 ReadByte = ();
RH_Byte2 ReadByte = ();
T_Byte1 ReadByte = ();
T_Byte2 ReadByte = ();
Checksum = ReadByte ();
/ / Check for error in reception Date
if (CheckSum == ((RH_Byte1 + RH_Byte2 + T_Byte1 + T_Byte2) & 0xFF))
{
message1[7] = T_Byte1/10 + 48;
message1[8] = T_Byte1%10 + 48;
message1[10] = T_Byte2/10 + 48;
message2[7] = RH_Byte1/10 + 48;
message2[8] = RH_Byte1%10 + 48;
message2[10] = RH_Byte2/10 + 48;
message1[11] = 178; // Degree symbol use 223 if Greek letter appears
Lcd_Cmd(_Lcd_Clear);
Lcd_Out(1, 1, message1);
Lcd_Out(2, 1, message2);
Lcd_Cmd ( _Lcd_Clear );
Lcd_Out ( 1, 1, message1 );
Lcd_Out ( 2, 1, message2 );
}
else {
Lcd_Cmd ( _Lcd_Clear );
Lcd_Out ( 1 , 1 , " Checksum Error ");
Lcd_Out ( 2, 1, "Trying Again ... ");
}
}
} while (1);
}
I thank the help.
Rodrigo Ravanelli Lopes
gentlemen
I need to migrate from 16f877 to 18F4520 18F4520 but does not have the same instructions that 16f made some changes based on the datsheet 18f but not working without going straight to the sensor response, I need to know if these changes I've made are correct if not 're correct me by favor.os points concerned the program are marked with a question.
sbit at RA0_bit Date ;
sbit at DataDir TRISA0_bit ;
message1 char [ ] = "Temp = 00.0 C " ;
message2 char [ ] = "RH = 00.0 % ;"
unsigned short TOUT = 0 , CheckSum , i ;
unsigned short T_Byte1 , T_Byte2 , RH_Byte1 , RH_Byte2 ;
StartSignal void ( ) {
DataDir = 0 / / Data port is output
Data = 0 ;
Delay_ms ( 25);
Date = 1 ;
Delay_us (30);
DataDir = 1 / / Data port is input
}
unsigned short CheckResponse ( ) {
TOUT = 0;
TMR2 = 0;
T2CON.TMR2ON = 1 / / start timer
while ( ! date && ! TOUT ) ;
if ( TOUT ) return 0;
else {
TMR2 = 0;
while ( date && ! TOUT ) ;
if ( TOUT ) return 0;
else {
T2CON.TMR2ON = 0;
return 1;
}
}
}
unsigned short ReadByte ( ) {
unsigned short a = 0 , t ;
DataDir = 1;
for (i = 0 : i 100 ) a | = 1 40us , Date is 1 / / I put 200 on the counter this correct?
}
return a ;
}
void interrupt () {
if ( PIR1.TMR2IF ) {
TOUT = 1;
T2CON.TMR2ON = 0 / / stop timer
PIR1.TMR2IF = 0 / / Clear TMR0 interrupt flag
}
}
void main ( ) {
unsigned short check ;
ANSEL = 0 / / Set AN pins to digital / how can I configure for PIC18F4520 with external clock 20mhz ?
ANSELH = 0 ; / how can I configure for PIC18F4520 ? with external clock 20mhz ?
C1ON_bit = 0 / / Disable comparators / how can I configure for PIC18F4520 with external clock 20mhz ?
C2ON_bit = 0 ; / how can I configure for PIC18F4520 with external clock 20mhz ? / /
these were my changes but not working is correct what I did ?
ADCON0 = 0x00 ; / / Configure AN pins to Digital
ADCON1 = 0b00001110 / / AN0 ENABLED
ADCON2 = 0b10000100 / / BUILD ALL CHANNELS , JUSTIFIED RIGHT UNLESS THE YEAR
ADCON0.ADON = 1 ;/ / Enables the convert
ADCON0.GO_NOT_DONE = 1 / / BEGIN CONVERSION
while ( ADCON0.GO_DONE ) ;/ / WAITING conversion finished
ADCON0.ADON = 0 ;/ / DISABLED THE CONVERTER
ADCON0.ADON = 1 ;/ / ENABLE THE CONVERTER
CCP1CON = 0 / / C1ON_bit = 0 / / Disable comparators
CCP2CON = 0 ;/ / C2ON_bit = 0;
end of my changes but it is not working , I made this change to migrate this correct ?
TRISB = 0;
TRISA = 0x01 , / / configure the input pins
TRISD = 0;
TRISB = 0;
TRISA = 0x01 ;
TRISD = 0, / / configure the input pins
INTCON.GIE = 1 / / Enable global interrupt
INTCON.PEIE = 1 / / Enable peripheral interrupt
/ / Configure Timer2 module
PIE1.TMR2IE = 1 / / Enable Timer2 interrupt
T2CON = 0 ; / / Prescaler 1:1 , and Timer2 is off INITIALLY
PIR1.TMR2IF = 0 / / Clear INT flag bit TMR
TMR2 = 0;
Lcd_Init ();
Lcd_Cmd ( _Lcd_Clear );
Lcd_Cmd ( _LCD_CURSOR_OFF );
do {
Delay_ms ( 1000) ;
StartSignal ();
CheckResponse check = ( ) ;
if ( ! check ) {
Lcd_Cmd ( _Lcd_Clear );
Lcd_Out ( 1, 1, " In response" );
Lcd_Out ( 2, 1, " from the sensor ");
}
else {
RH_Byte1 ReadByte = ();
RH_Byte2 ReadByte = ();
T_Byte1 ReadByte = ();
T_Byte2 ReadByte = ();
Checksum = ReadByte ();
/ / Check for error in reception Date
if (CheckSum == ((RH_Byte1 + RH_Byte2 + T_Byte1 + T_Byte2) & 0xFF))
{
message1[7] = T_Byte1/10 + 48;
message1[8] = T_Byte1%10 + 48;
message1[10] = T_Byte2/10 + 48;
message2[7] = RH_Byte1/10 + 48;
message2[8] = RH_Byte1%10 + 48;
message2[10] = RH_Byte2/10 + 48;
message1[11] = 178; // Degree symbol use 223 if Greek letter appears
Lcd_Cmd(_Lcd_Clear);
Lcd_Out(1, 1, message1);
Lcd_Out(2, 1, message2);
Lcd_Cmd ( _Lcd_Clear );
Lcd_Out ( 1, 1, message1 );
Lcd_Out ( 2, 1, message2 );
}
else {
Lcd_Cmd ( _Lcd_Clear );
Lcd_Out ( 1 , 1 , " Checksum Error ");
Lcd_Out ( 2, 1, "Trying Again ... ");
}
}
} while (1);
}
I thank the help.
Rodrigo Ravanelli Lopes