Hi,
Can someone please kindly tell me what is wrong in the code I've written?
I am using MPLAB and C18 compiler. I was using HTC compiler but I had to swap to C18 compiler so most the code I tried to change as far as you can see here:
*Most of the errosr ask me to define functions
Can someone please kindly tell me what is wrong in the code I've written?
I am using MPLAB and C18 compiler. I was using HTC compiler but I had to swap to C18 compiler so most the code I tried to change as far as you can see here:
*Most of the errosr ask me to define functions
Rich (BB code):
#include<p18f248.h>
#include<stdio.h>
#pragma config WDT = OFF //Disable watchdog timer
#pragma config OSC = HS // High Speed mode
unsigned i;
unsigned flag;
void initialpriority()
{
INTCON=0x00; //Set interrupts disable
ADCON1=0x07; //Set PORTA to digital mode
PIE1=0; //Peripheral1 Interrupt disabled
PIE2=0; //Peripheral2 Interrupt disabled
PIE3=0; //Peripheral2 Interrupt disabled
}
void int_can() //Set I/O Ports
{
TRISB=(TRISB|0x08)&0xFB; //Set RB2 as CAN Tx and RB3 as CAN Rx
CANCON=0x80; //Enter CAN CONfiguration mode
while((CANSTAT&0x80)==0) //CANCON not enabled then wait
{
;
}
CIOCON=0x00; //Enable CAN I/O ports
//Baud rate control register=500kbit/sec,FOSC=20MHz, TQ=20Tq
BRGCON1=0x80; //Set Synchronisation Jump Width Time=3xTQ (10)
BRGCON2=0xbe; //Set Phase Segment Time1=8xTQ, bit 5-3
//Propagation time 7xTQ, bit2-0
BRGCON=0x43; //Set Phase Segment Time2=4xTQ, bit 2-0,bit 5-3 read '0'
//Receive buffer 0
RXB0CON=0x60; //Receive all valid messages(212)~acceptance filter 0
RXB0DLC=0x08; //Data Lenght Config for 11-bit intentifier is 8 bytes
RXB0SIDH=0xfd; //Shared Implemented Data access bank high
RXB0SIDL0x00; //Shared Implemented Data access bank low
//Set all receive data field byte (RXB0D0 to RXBOD7) clear;
RXB0D0=0x00;
RXB0D1=0x00;
RXB0D2=0x00;
RXB0D3=0x00;
RXB0D4=0x00;
RXB0D5=0x00;
RXB0D6=0x00;
RXB0D8=0x00;
RXF0SIDH=0xfd; //Filter Shared Implemented Data access High
RXF0SIDH=0x00; //Filter Shared Implemented Data access Low
RXM0SIDH=0xff; //Mask shared Implemented Data access High
RXM0SIDH=0xe0; //Mask shared Implemented Data access Low ~@~@~##~^*
CANCON=0x00; //Enter CAN CONfiguration normal mode
while((CANSTAT&0xe0)!=0) //Wait to enter normal mode
{
;
}
PIE3=0x00; //Clear all Peripheral3 Interrupts
PIE3=PIE3|0x01; //Enable CAN receive Peripheral Interrupt
IPR3=IPR3|0x01; //Set Interrupt Priority Register Rx high
}
void int_usart()
{
TRISC=0x80; ////Set R6 as CAN Tx and R7 as CAN Rx
TXSTA=0x04; //Set Transmit Status and Control register to asynchronous mode
RCSTA=0x80; //Enable Receive Status and control register to read bits of data
SPBRG=0x0c; //Set baud rate generator to high speed mode~@~@~@#'#'***
PIE1=PIE1|0x10; //Peripheral Interrupt enabled (188)
TXIP=0; //Transmit Interrupt Priority bit low
TXEN=1; //Transmit status enabled
}
void interrupt_CANRx()
{
flag=1; //Set interrupt high if CAN data received
RXB0IF=0; //Clear the interrupt flag
RXB0FUL=0; //Set to receive new message
}
void main(void)
{
initialpriority(); //Call function
int_can(); //Call function
int_usart(); //Call function
IPEN=1; //Interrupt priority enabled
INTCON=INTCON|0xc0; // Enable interrupts
TRISA=0x00; //Set RA3, RA0 as inputs| RA5, RA4 as outputs
while(1) //Wait to receive interrupt flag
{
while(flag==0) //for flag=0¬
{
PORTA=0x00; //Set I/O ports to 0
}
while(flag==1) //for flag=1¬
PORTA=0x01; //Set PORTA as output (LED_ON)
flag=0; //Clear output data to initialize PORTA¬
//Transmit the data bits one by one
TXREG=RGB0D0; //While data bits in receiver buffer
for(i=0;i<100;i++){;} // Time delay between read byte data
TXREG=RXB0D1;
for(i=0;i<100;i++){;} // Time delay between read byte data
TXREG=RXB0D2;
for(i=0;i<100;i++){;} // Time delay between read byte data
TXREG=RXB0D3;
for(i=0;i<100;i++){;} // Time delay between read byte data
TXREG=RXB0D4;
for(i=0;i<100;i++){;} // Time delay between read byte data
TXREG=RXB0D5;
for(i=0;i<100;i++){;} // Time delay between read byte data
TXREG=RXB0D6;
for(i=0;i<100;i++){;} // Time delay between read byte data
TXREG=RXB0D7;
for(i=0;i<100;i++){;} // Time delay between read byte data
}
}
Last edited by a moderator: