What I intend to do is as follows
Everytime interrupt occur, UARTSendF = 1and cnt80002 will increase by 1. Whenever UARTSendF = 1, cnt80002 value will be sent through UART. If everything works well, i will receive values from 0 upto 7999 from serial port. At the same time, cnt8000 will keep tract how many times PIC have sent to UART. So, if everything is correct, cnt8000 should equals to cnt80002 at all times.
Below is the cnt80002 i received from UART:
1
21
54
87
120
162
204
What i hope to receive is as follows:
1
2
3
4
5
6
7
Here is my code:
I know i did something wrong but I do not know what it is..
Everytime interrupt occur, UARTSendF = 1and cnt80002 will increase by 1. Whenever UARTSendF = 1, cnt80002 value will be sent through UART. If everything works well, i will receive values from 0 upto 7999 from serial port. At the same time, cnt8000 will keep tract how many times PIC have sent to UART. So, if everything is correct, cnt8000 should equals to cnt80002 at all times.
Below is the cnt80002 i received from UART:
1
21
54
87
120
162
204
What i hope to receive is as follows:
1
2
3
4
5
6
7
Here is my code:
Code:
#pragma config OSC=HSPLL
#pragma config PWRT=ON
#pragma config BOREN=OFF
#pragma config WDT=OFF
#pragma config MCLRE=ON
#pragma config PBADEN=OFF
#pragma config LVP=OFF
#pragma config XINST=OFF
#define CLOCK_FREQ (40000000) // Hz
/////////////////////////////////////
// FUNCTION PROTOTYPE
/////////////////////////////////////
void low_isr();
void InitInterrupt();
void InitTimer2();
void InitADC();
void InitUART();
void Test();
/////////////////////////////////////
// GLOBAL VARIABLE
/////////////////////////////////////
char results;
char UARTSendF = 0;
unsigned int cnt8000;
unsigned int cnt80002;
/////////////////////////////////////
// SETUP ISR VECTOR
/////////////////////////////////////
#pragma code low_vector=0x18 //setup the ISR vector
void low_interrupt (){
_asm GOTO low_isr _endasm //jump to interrupt handler
}
#pragma code
/////////////////////////////////////
// ISR
/////////////////////////////////////
#pragma interruptlow low_isr //the ISR
void low_isr()
{
if (PIR1bits.TMR2IF == 1)
{
PIR1bits.TMR2IF=0;
Test();
}
}
#pragma code
void main()
{
InitInterrupt();
InitTimer2();
InitADC();
InitUART();
TRISB = 0x00;
while (1)
{
if (UARTSendF)
{
if (cnt8000 < 8000)
{
printf("%d\r\n", cnt80002);
UARTSendF = 0;
cnt8000 +=1;
}
else
printf("STOP");
}
}
}
void InitInterrupt()
{
PIE1bits.TMR2IE = 1; //enable Timer2 interrupt
INTCONbits.PEIE = 1; //enable peripheral interrupts
INTCONbits.GIE = 1; //enable glogal interrupts
IPR1bits.TMR2IP = 0; // low priority
}
void InitTimer2()
{
T2CON = 0b01001000;
PR2 = 124; // 125 counts including 0
T2CONbits.TMR2ON = 1;
}
void InitADC()
{
ADCON0 = 0b00000001;
ADCON1 = 0b00001110;
ADCON2 = 0b00010010;
DDRAbits.RA0 = 1;
}
void InitUART()
{
DDRCbits.RC6 = 0;
DDRCbits.RC7 = 1;
SPBRG = 64;
TXSTA = 0b00100010;
RCSTA = 0b10010000;
BAUDCON = 0b00000000;
TRISC = 0b10000001;
}
void Test()
{
UARTSendF = 1;
cnt80002 += 1;
}
I know i did something wrong but I do not know what it is..