So I trying to learn to program in C
My boss has sent me a file that compiles for the PIC16f648, and I am trying to convert to the PIC16f1825, but currently struggling, especially with the sendbits commands...
Can anyone please guide me in the right direction?
My boss has sent me a file that compiles for the PIC16f648, and I am trying to convert to the PIC16f1825, but currently struggling, especially with the sendbits commands...
Can anyone please guide me in the right direction?
Code:
// CONFIG1
#pragma config FOSC = INTOSC // Oscillator Selection (INTOSC oscillator: I/O function on CLKIN pin)
#pragma config WDTE = OFF // Watchdog Timer Enable (WDT disabled)
#pragma config PWRTE = ON // Power-up Timer Enable (PWRT enabled)
#pragma config MCLRE = OFF // MCLR Pin Function Select (MCLR/VPP pin function is digital input)
#pragma config CP = OFF // Flash Program Memory Code Protection (Program memory code protection is disabled)
#pragma config CPD = OFF // Data Memory Code Protection (Data memory code protection is disabled)
#pragma config BOREN = OFF // Brown-out Reset Enable (Brown-out Reset disabled)
#pragma config CLKOUTEN = OFF // Clock Out Enable (CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin)
#pragma config IESO = OFF // Internal/External Switchover (Internal/External Switchover mode is disabled)
#pragma config FCMEN = OFF // Fail-Safe Clock Monitor Enable (Fail-Safe Clock Monitor is disabled)
// CONFIG2
#pragma config WRT = OFF // Flash Memory Self-Write Protection (Write protection off)
#pragma config PLLEN = OFF // PLL Enable (4x PLL disabled)
#pragma config STVREN = ON // Stack Overflow/Underflow Reset Enable (Stack Overflow or Underflow will cause a Reset)
#pragma config BORV = LO // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), low trip point selected.)
#pragma config LVP = ON // Low-Voltage Programming Enable (Low-voltage programming enabled)
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
#define _XTAL_FREQ 4000000
#include <xc.h>
char digitcode[10] = {126,24,109,61,27,55,119,28,127,31}; // setup the 7 segment data that correspond to the digits 0..9
// set up holding values for each 7segment display
// set all segments ON for initial test
char A0 = 255;
char A1 = 255;
char A2 = 255;
char A3 = 255;
char B0 = 255;
char B1 = 255;
char B2 = 255;
char B3 = 255;
char C0 = 255;
char C1 = 255;
char C2 = 255;
char C3 = 255;
char bitcnt = 0;
char RXbyte;
char RXbytetemp;
char RXchan;
int RXcnt;
char DPRX;
void LatchIt(){
__delay_us(50);
RC1 = 1; // latch pin high
__delay_us(50);
RC1 = 0; // latch pin low
}
void SendBits(char digit1, char digit2,char digit3, char digit4){
// this function sends the bits for display
//------------------ 1st digit -----------------------------
for (bitcnt=0;bitcnt<=7;bitcnt++){
__delay_us(50);
RA2 = 1; // clock pin high
__delay_us(50);
if ((digit1 & 0x80) == 0x80){
RC0 = 1; // data pin high
}
else{
RC0 = 0; // data pin low
}
digit1 = (unsigned char)(digit1 << 1);
__delay_us(50);
RA2 = 0; // clock pin low
}
//---------------------- 2nd digit --------------------------
for (bitcnt=0;bitcnt<=7;bitcnt++){
__delay_us(50);
RA2 = 1; // clock pin high
__delay_us(50);
if ((digit2 & 0x80) == 0x80){
RC0 = 1; // data pin high
}
else{
RC0 = 0; // data pin low
}
digit2 = digit2 << 1;
__delay_us(50);
RA2 = 0; // clock pin low
}
//----------------------- 3rd digit ------------------------
for (bitcnt=0;bitcnt<=7;bitcnt++){
__delay_us(50);
RA2 = 1; // clock pin high
__delay_us(50);
if ((digit3 & 0x80) == 0x80){
RC0 = 1; // data pin high
}
else{
RC0 = 0; // data pin low
}
digit3 = digit3 << 1;
__delay_us(50);
RA2 = 0; // clock pin low
}
//----------------------- 4th digit -------------------------
for (bitcnt=0;bitcnt<=7;bitcnt++){
__delay_us(50);
RA2 = 1; // clock pin high
__delay_us(50);
if ((digit4 & 0x80) == 0x80){
RC0 = 1; // data pin high
}
else{
RC0 = 0; // data pin low
}
digit4 = digit4 << 1;
__delay_us(50);
RA2 = 0; // clock pin low
}
// return clock to high to finish
__delay_us(50);
RA2 = 1; // clock pin high
}
void main(void) {
// set up i/o
CM1CON0 = 0x00; //comparitor off
CM1CON1 = 0x00; //comparitor off
ANSELA = 0x00;
ANSELC = 0x00;
PORTA = 0x0F;
TRISA = 0x00; // all portA are outputs
RA2 = 1; // clock pin high
TRISC = 240; // set RX & TX pins to inputs
RC1 = 0; // latch pin low
RC0 = 1; // data pin high
BRGH = 1;
SPBRG = 25; // set baud rate to 9600 Baud (BRGH = 1) 4MHz internal XTAL
SYNC = 0; // async comms
TX9 = 0; // 8 bit TX
TXEN = 1; // enable TX
SPEN = 1; // enable serial port
CREN = 1; // enable RX
RX9 = 0; // 8 bit RX
CREN = 0; // reset any errors
CREN = 1;
// at this point we need to check for received data
// ignore all chars except 0..9, A,B,C, space, decimalpoint and carriage return
// only update displays when a valid sequence of recieved data is received
// A123.4 <CR> is a minumum type of TX
//A123.4B567.8C901.2 <CR> is a longer type
// a decimal point will add 128 to the next data value
SendBits(0,0,0,0);
SendBits(0,0,0,0);
SendBits(0,0,0,0);
LatchIt();
// 2 second delay
for (bitcnt=0;bitcnt<=20;bitcnt++){
__delay_ms(100);
CLRWDT();
}
SendBits(0,0,0,0);
SendBits(0,0,0,0);
SendBits(0,0,0,0);
LatchIt();
__delay_ms(200);
SendBits(1,1,1,1);
SendBits(1,1,1,1);
SendBits(1,1,1,1);
LatchIt();
__delay_ms(200);
SendBits(3,3,3,3);
SendBits(3,3,3,3);
SendBits(3,3,3,3);
LatchIt();
__delay_ms(200);
SendBits(7,7,7,7);
SendBits(7,7,7,7);
SendBits(7,7,7,7);
LatchIt();
__delay_ms(200);
SendBits(15,15,15,15);
SendBits(15,15,15,15);
SendBits(15,15,15,15);
LatchIt();
__delay_ms(200);
SendBits(31,31,31,31);
SendBits(31,31,31,31);
SendBits(31,31,31,31);
LatchIt();
__delay_ms(200);
SendBits(63,63,63,63);
SendBits(63,63,63,63);
SendBits(63,63,63,63);
LatchIt();
__delay_ms(200);
SendBits(127,127,127,127);
SendBits(127,127,127,127);
SendBits(127,127,127,127);
LatchIt();
__delay_ms(200);
SendBits(255,255,255,255);
SendBits(255,255,255,255);
SendBits(255,255,255,255);
LatchIt();
// 2 second delay to check operation of LCD segments and PCB LED
for (bitcnt=0;bitcnt<=20;bitcnt++){
__delay_ms(100);
CLRWDT();
}
RC3 = 1; // LED Off
__delay_ms(800);
RC3 = 0; // LED = OFF RC3 = 1; // LED Off
__delay_ms(800);
RC3 = 1; // LED = OFF
RXchan = 'X'; // select no channel (i.e. A.B or C)
// loop forever
while(1==1){
CLRWDT();
if (TRMT == 1){
TXREG = 0x55;
}
// need to check for received data and act upon it if a valid sequence
// if a received error then rest the comms
// have we received any comms
if (RCIF == 1){
if (OERR == 1){
// if here we had an overrun error on RX comms
// read and throw away any byte received
RXbyte = RCREG;
// so reset comms
CREN = 0; // reset any errors
CREN = 1;
}
else{
// if here then a character has arrived
// valid characters are 0..9, decimal point, Carriage Return,
// A, B, C
// throw away all others
RXbyte = RCREG;
// any of the chars A ,B or C restart the RXcnt
if (RXbyte == 'A'){
RXcnt = 0;
DPRX = 0;
RXchan = RXbyte;
//clear all chars for channel
A0=0;
A1=0;
A2=0;
A3=0;
}
if (RXbyte == 'B'){
RXcnt = 0;
DPRX = 0;
RXchan = RXbyte;
B0=0;
B1=0;
B2=0;
B3=0;
}
if (RXbyte == 'C'){
RXcnt = 0;
DPRX = 0;
RXchan = RXbyte;
C0=0;
C1=0;
C2=0;
C3=0;
}
if (RXbyte == '.'){
DPRX = 1; // acknowledge the decimal point
}
if ((RXbyte >= '0') & (RXbyte <= '9')){
RXbytetemp = RXbyte - '0'; // convert to number
RXbytetemp = digitcode[RXbytetemp];// convert to digit code
if (DPRX == 1){
RXbytetemp = RXbytetemp + 0x80;// add DP to character
DPRX = 0; // reset DPRX has it as been used
}
// need to put the value into the correct channel
if (RXchan == 'A'){
//this was the last channel selected
if (RXcnt == 0){
A3 = RXbytetemp;
}
if (RXcnt == 1){
A2 = RXbytetemp;
}
if (RXcnt == 2){
A1 = RXbytetemp;
}
if (RXcnt == 3){
A0 = RXbytetemp;
}
}
if (RXchan == 'B'){
//this was the last channel selected
if (RXcnt == 0){
B3 = RXbytetemp;
}
if (RXcnt == 1){
B2 = RXbytetemp;
}
if (RXcnt == 2){
B1 = RXbytetemp;
}
if (RXcnt == 3){
B0 = RXbytetemp;
}
}
if (RXchan == 'C'){
//this was the last channel selected
if (RXcnt == 0){
C3 = RXbytetemp;
}
if (RXcnt == 1){
C2 = RXbytetemp;
}
if (RXcnt == 2){
C1 = RXbytetemp;
}
if (RXcnt == 3){
C0 = RXbytetemp;
}
}
if (RXcnt<5){
RXcnt++;
// if RXcnt = 5 then character ignored
}
}
if (RXbyte== 0x0D){
if (RC3 == 1){
RC3 = 0;
}
else {
RC3 = 1;
}
// carriage return received so send out what we have got
SendBits(A0,A1,A2,A3);
SendBits(B0,B1,B2,B3);
SendBits(C0,C1,C2,C3);
LatchIt();
RXchan='X'; // just to prevent RX being stored without
// a channel A,B,C being sent first
}
}
}
}
return;
}