shaneelal
Member level 1
putsusart
I'm not experienced with writting code.I"m using the c18 compiler and a PIC 18f452.I used the manual to help me. This code was written to recieve an interrupt every 2 seconds from a 555 timer. When the interrupt is recieved, the readings of two sensors are taken using ADC. A wirespeed is also read from another device. These results are then displayed onto an LCD screen aswell as a tv screen. I'm new to interrupts.
The ISR function is kind of long,is it too long?
How can I make my code more efficient? Can I write some of the constants to the ROM of the PIC? Is the ISR function set up properly? If there is anything wrong please tell me. Any tips would be greatly appreciated.
I'm not experienced with writting code.I"m using the c18 compiler and a PIC 18f452.I used the manual to help me. This code was written to recieve an interrupt every 2 seconds from a 555 timer. When the interrupt is recieved, the readings of two sensors are taken using ADC. A wirespeed is also read from another device. These results are then displayed onto an LCD screen aswell as a tv screen. I'm new to interrupts.
The ISR function is kind of long,is it too long?
How can I make my code more efficient? Can I write some of the constants to the ROM of the PIC? Is the ISR function set up properly? If there is anything wrong please tell me. Any tips would be greatly appreciated.
Code:
#include <p18f452.h>
#include <xlcd.h>
#include <delays.h>
#include <stdlib.h>
#include <usart.h>
#include <portb.h>
#include <adc.h>
#include <string.h>
/* Set configuration bits for use with ICD2 / PICDEM2 PLUS Demo Board:
* - set XT oscillator
* - disable watchdog timer
* - disable low voltage programming
*/
#pragma romdata CONFIG
_CONFIG_DECL(_CONFIG1H_DEFAULT & _OSC_XT_1H,
_CONFIG2L_DEFAULT,
_CONFIG2H_DEFAULT & _WDT_OFF_2H,
_CONFIG3H_DEFAULT,
_CONFIG4L_DEFAULT & _LVP_OFF_4L,
_CONFIG5L_DEFAULT,
_CONFIG5H_DEFAULT,
_CONFIG6L_DEFAULT,
_CONFIG6H_DEFAULT,
_CONFIG7L_DEFAULT,
_CONFIG7H_DEFAULT);
#pragma romdata
void system(void);
void DelayFor18TCY( void )
{
Nop();
Nop();
Nop();
Nop();
Nop();
Nop();
Nop();
Nop();
Nop();
Nop();
Nop();
Nop();
}
void DelayPORXLCD( void )
{
Delay1KTCYx(60); //Delay of 15ms
return;
}
void DelayXLCD( void )
{
Delay1KTCYx(20); //Delay of 5ms
return;
}
void main( )
{
//config RBO for interrupts
OpenRB0INT( PORTB_CHANGE_INT_ON & PORTB_CHANGE_INT_ON & RISING_EDGE_INT & PORTB_PULLUPS_ON );
// configure external LCD
OpenXLCD( FOUR_BIT & LINES_5X7 );//four bit mode,multiple line display
// configure USART,DOUBLE CHECK
//interrupts of on transmit and reciept
//asynchronous mode
/*//8-bit transmisson
//not recieving anything,should i leave out???
//spbrg= 25*/
OpenUSART( USART_TX_INT_OFF & USART_RX_INT_OFF &
USART_ASYNCH_MODE & USART_EIGHT_BIT &
USART_BRGH_HIGH &
USART_CONT_RX,25);
OpenADC(ADC_FOSC_8 & ADC_RIGHT_JUST &
ADC_8ANA_0REF,ADC_CH0 &
ADC_INT_OFF);
/*OpenADC(ADC_FOSC_8 & ADC_RIGHT_JUST &
ADC_8ANA_0REF & ADC_CH1 &
ADC_INT_OFF);*/
INTCONbits.GIE = 1;//do I have to put,yes from pg58
while (1)
{
}
}
#pragma interrupt system //save=PRODH, PRODL,section( ".tmpdata" )
void system(void)
{
int current_adc;
int voltage_adc;
int voltage;
int current;
char speed[7];
char voltage_str[7];
char current_str[7];
int convFactor = (5.0 / 1024);
char sp_units[10] = "mm/sec";
char vol_units[10] = "Volts";
char cur_units[10] = "Amps";
char welcome[20] = "Welding Monitor";
char cursorL1[5] = "{A17" ;
char cursorL2[7] = "{C0202" ;
char cursorL5[7] = "{C0204";
char cursorL7[7] = "{C0206";
char cursorL9[9] = "{C0209";
INTCONbits.INT0IE = 0; //stop interrupting continuously
SetChanADC(ADC_CH0); //select voltage channel
Delay10TCYx(3);
ConvertADC();
while( BusyADC());
voltage_adc=ReadADC();
voltage = (voltage_adc * convFactor *100);
SetChanADC( ADC_CH1 );//select current channel
Delay10TCYx(3);
ConvertADC();
while( BusyADC());
current_adc=ReadADC();
current = (current_adc *1.2* convFactor *600);
while(!DataRdyUSART());
getsUSART(speed , 7);//obtain speed
while (BusyUSART()); //TEST TO SEE IF CAN REMOVE
/***************write to LCD*******************/
while(BusyXLCD());
SetDDRamAddr(0x1C);
while(BusyXLCD());
putsXLCD( sp_units );
while(BusyXLCD());
SetDDRamAddr(0x14);
while(BusyXLCD());
putsXLCD(speed);
while(BusyXLCD());
SetDDRamAddr(0x48);
while(BusyXLCD());
putsXLCD( cur_units );
while(BusyXLCD());
SetDDRamAddr(0x40);
itoa(current,current_str);
while(BusyXLCD());
putsXLCD(current_str);
while(BusyXLCD());
SetDDRamAddr(0x08);
while(BusyXLCD());
putsXLCD( vol_units );
while(BusyXLCD());
SetDDRamAddr(0x00);
itoa(voltage,voltage_str);
while(BusyXLCD());
putsXLCD(voltage_str);
/************write to tv screen*************************/
while (BusyUSART());
putsUSART(cursorL1 ); //clear screen
while (BusyUSART());
putsUSART( cursorL2 );
while (BusyUSART());
putsUSART( welcome );
while (BusyUSART());
putsUSART( cursorL5 );
while (BusyUSART());
putsUSART( speed );
while (BusyUSART());
putsUSART( sp_units );
putsUSART( cursorL7 );
while (BusyUSART());
putsUSART( current_str );
while (BusyUSART());
putsUSART( cur_units );
putsUSART( cursorL9 );
while (BusyUSART());
putsUSART( voltage_str );
while (BusyUSART());
putsUSART( vol_units );
}