Software Main Code (With kit-board)
/*
* File: Final695Board_Main.c
* Author: S. Flores, M. Mcgonigle, E. Jesse, A. Nguyen
*
* Created on February 8, 2015, 8:09 PM
*/
#include <xc.h>
#include <math.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <sys/attribs.h>
#include "configbitsrev2014vC.h"
#include "TouchScreenIDs.h"
/*Interrupt Flag*/
#define GPSFlag IFS0bits.U1RXIF
/*SPI4 Chip Select*/
#define CS_High LATBbits.LATB8 = 1;
#define CS_Low LATBbits.LATB8 = 0;
/*Global Variables*/
//GPS Module
unsigned char Sentence[35];
unsigned char Message[35];
int Mode;
unsigned char Ck_a=0;
unsigned char Ck_b=0;
unsigned int Complete=0;
unsigned int i;
unsigned long Time;
unsigned long TimeStart;
unsigned char Time_Hour;
unsigned char Time_Minute;
unsigned char Time_Second;
//LCD Module
unsigned char ACK;
unsigned char OBJ_ID;
unsigned char OBJ_Index;
unsigned long LapTime = 0;
unsigned char LapHours = 0;
unsigned char LapMinutes = 0;
unsigned char LapSeconds = 0;
char LapHoursString[50];
char LapMinutesString[50];
char LapSecondsString[50];
char TimeString[250];
int LapTimeCount = 0;
int Racing = 0;
int Testing = 0;
//Save Module
int Save = 0;
int Clear = 0;
int Download = 0;
/*Function Prototypes*/
//GPS Functions
void Initialize_UART1(unsigned long Rate);
unsigned char GetUART1(void);
void PutUART1(unsigned char Value);
void Send_Command (unsigned char* GPS_Signal, int Length);
unsigned long Assign(unsigned int Index);
//Temperature Functions
void Initialize_A2D(void);
unsigned short A2D_Convert(void);
void Initialize_Timer1(void);
//LCD Functions
void serialinitLCD(unsigned long baudrate);
unsigned char setLCDvalue(char device[], unsigned char value);
unsigned char setLCDtext(char device[], char text[]);
void putLCD(unsigned char input);
unsigned char getLCD(void);
//Save Module
void Initialize_SPI4(void);
unsigned char SPI_Send(unsigned char Command);
void Chip_Setup(void);
void Chip_Clear(void);
void SPI_Wait(unsigned char Command);
//Download Module
void Initialize_UART6(unsigned long Rate);
void PutUART6(unsigned char Value);
unsigned char GetUART6(void);
//define interrupt service routine
void __ISR(49, IPL7AUTO) ButtonPressHandle(void){
unsigned char temp;
ACK=getLCD();
OBJ_ID = 0x00;
OBJ_Index=0x00;
if (ACK != 0x06){
OBJ_ID=getLCD();
OBJ_Index=getLCD();
temp=getLCD();
temp=getLCD();
temp=getLCD();
}
if(OBJ_Index == 0x09){
if(LapTimeCount == 0){
LapTime = TimeStart/10;
}
else{
LapTime = Time/10;
}
LapHours = (LapTime/1000000);
LapMinutes = (LapTime -((LapHours)*1000000))/10000;
LapSeconds = (LapTime - ((LapHours)*1000000)-(LapMinutes*10000))/100;
ltoa(LapHoursString,LapHours,10);
ltoa(LapMinutesString,LapMinutes,10);
ltoa(LapSecondsString,LapSeconds,10);
if(LapTimeCount > 0){
strcat(TimeString,"\n");
strcat(TimeString,LapHoursString);
}
else{
strcpy(TimeString,LapHoursString);
}
strcat(TimeString,":");
strcat(TimeString,LapMinutesString);
strcat(TimeString,":");
strcat(TimeString,LapSecondsString);
setLCDtext("Time",TimeString);
LapTimeCount++;
}
else if(OBJ_Index == 0x08){
Save = 1-Save;
Racing = 1;
Testing = 0;
}
else if(OBJ_Index == 0x0A){
Save = 1-Save;
Racing = 0;
Testing = 1;
}
else if(OBJ_ID == Form){
Download = 1;
//PutUART6('I');
}
IFS2bits.U4RXIF = 0; //Reset interrupt flag
}
void __ISR(24,IPL5AUTO) GPSinterrupt(void){
unsigned char UARTChar1;
UARTChar1 = GetUART1();
GPSFlag = 0;
switch (Mode){
case 0: //Check for preamble byte 1
if (UARTChar1 == 0xD0){
Mode = 1;
}
else{
Mode = 0;
}
break;
case 1://Check for preamble byte 2
if (UARTChar1 == 0xDD){
Ck_a = 0;
Ck_b = 0;
Mode = 2;
}
else{
Mode = 0;
}
break;
case 2://Check for payload
if (UARTChar1 == 0x20){
Ck_a = Ck_a + UARTChar1;
Ck_b = Ck_b + Ck_a;
i = 0;
Mode = 3;
}
else{
Mode = 0;
}
break;
case 3://Store Data
Mode=3;
Sentence[i] = UARTChar1;
Ck_a = Ck_a + UARTChar1;
Ck_b = Ck_b + Ck_a;
i++;
if (i == 32){
Mode = 4;
}
break;
case 4://Confirm checksum 1
if (Ck_a == UARTChar1){
Mode = 5;
}
else{
Mode = 0;
}
break;
case 5://Confirm checksum 2
if (Ck_b == UARTChar1){
Complete = 1;
}
Mode = 0;
break;
default:
Mode = 0;
break;
}
}
int main() {
/*Declare Module Variables*/
//GPS Module
unsigned char Binary_Mode[] = "$PGCMD,16,0,0,0,0,0*6A\r\n";
unsigned char Check_Response = 0;
unsigned long Latitude;
unsigned long Longitude;
unsigned long GroundSpeed;
unsigned long Heading;
unsigned long Date;
unsigned char Fix;
int FirstRun = 0;
//Temperature Module
unsigned short Temperature = 0;
//RPM Module
unsigned short RPM1Old = 0;
unsigned short RPM1New = 0;
unsigned short RPM1 = 0;
//LCD Module
unsigned char Speed = 0;
//Save Module
int j = 0;
unsigned char SPI_Read = 0;
unsigned short Records = 0;
unsigned long SaveAddressValue = 0;
unsigned char High_Byte = 0;
unsigned char Mid_Byte1 = 0;
unsigned char Mid_Byte2 = 0;
unsigned char Low_Byte = 0;
//Download Module
unsigned long DownloadAddressValue = 0;
unsigned char Records_High = 0;
unsigned char Records_Low = 0;
unsigned char Begin_Read = 0;
unsigned char RPM1_High_Byte = 0;
unsigned char RPM1_Low_Byte = 0;
unsigned char Temp_High_Byte = 0;
unsigned char Temp_Low_Byte = 0;
unsigned char Speed_High_Byte = 0;
unsigned char Speed_Mid_Byte1 = 0;
unsigned char Speed_Mid_Byte2 = 0;
unsigned char Speed_Low_Byte = 0;
unsigned char Lat_High_Byte = 0;
unsigned char Lat_Mid_Byte1 = 0;
unsigned char Lat_Mid_Byte2 = 0;
unsigned char Lat_Low_Byte = 0;
unsigned char Long_High_Byte = 0;
unsigned char Long_Mid_Byte1 = 0;
unsigned char Long_Mid_Byte2 = 0;
unsigned char Long_Low_Byte = 0;
unsigned char Date_High_Byte = 0;
unsigned char Date_Mid_Byte1 = 0;
unsigned char Date_Mid_Byte2 = 0;
unsigned char Date_Low_Byte = 0;
unsigned char Time_High_Byte = 0;
unsigned char Time_Mid_Byte1 = 0;
unsigned char Time_Mid_Byte2 = 0;
unsigned char Time_Low_Byte = 0;
unsigned char LapTime_High_Byte = 0;
unsigned char LapTime_Mid_Byte1 = 0;
unsigned char LapTime_Mid_Byte2 = 0;
unsigned char LapTime_Low_Byte = 0;
/*Initialize Modules*/
//GPS Module
Initialize_UART1(38400);
Send_Command(Binary_Mode, 24);
//Temperature Module
Initialize_A2D();
//RPM Module
Initialize_Timer1();
//LCD_Module
serialinitLCD(9600);
//Save Module
Initialize_SPI4();
//Download Module
Initialize_UART6(57600);
/*Enable Interrupts*/
//enable multivector interrupts
INTCONbits.MVEC=1;
//GPS Module
IEC0bits.U1RXIE=1;
IPC6bits.U1IP=5;
IPC6bits.U1IS=1;
IFS0bits.U1RXIF = 0;
//LCD Module
IEC2bits.U4RXIE=1;
IPC12bits.U4IP=7;
//Enable Global Interrupts
asm("ei");
//Save = 1;
Clear = 1;
while (1){
if(Save){
IEC0bits.U1RXIE=1;
FirstRun = 1;
LapTimeCount = 0;
strcpy(TimeString,"\0");
if(Clear){
Chip_Setup();
Chip_Clear();
SPI_Wait(0x00);
Clear = 0;
Complete = 0;
TMR1 = 0;
Records = 0;
SaveAddressValue = 0x05;
}
while(Save){
if(Complete){
/*Get Data*/
//GPS Module
GroundSpeed = Assign(15);
Latitude = Assign(3);
Longitude = Assign(7);
Date = Assign(25);
Time = Assign(29);
Fix = Sentence[219];
if(FirstRun){
TimeStart = Time;
FirstRun = 0;
}
GroundSpeed = (float)GroundSpeed * 2.23694;
//Temperature Module
Temperature = A2D_Convert();
//RPM Module
RPM1New = TMR1;
if(RPM1New < RPM1Old){
RPM1 = (65000-RPM1Old+RPM1New)*15;
}else{
RPM1 = (RPM1New - RPM1Old)*15;
}
RPM1Old = RPM1New;
/*Display Data*/
/* if(Racing){
setLCDvalue("MPH",GroundSpeed);
setLCDvalue("RPM",RPM1);
}
else if(Testing){
*/ setLCDvalue("TEMP",Temperature-50);
setLCDvalue("RPM",RPM1);
setLCDvalue("MPH",GroundSpeed);
//}
Complete = 0;
//TMR1 = 0;
/*Write Data to Memory*/
//Enable Write
CS_Low;
SPI_Send(0x06);
CS_High;
//initiate auto address increment and write RPM
CS_Low;
SPI_Send(0xAD);
SPI_Send(SaveAddressValue >> 16);
SPI_Send(SaveAddressValue >>8);
SPI_Send(SaveAddressValue);
High_Byte = RPM1 >> 8;
Low_Byte = RPM1;
SPI_Send(High_Byte);
SPI_Send(Low_Byte);
CS_High;
SPI_Wait(0x42);
//Continue auto address increment and write temperature
CS_Low;
SPI_Send(0xAD);
High_Byte = Temperature >> 8;
Low_Byte = Temperature;
SPI_Send(High_Byte);
SPI_Send(Low_Byte);
CS_High;
SPI_Wait(0x42);
//Continue auto address incremtn and write speed
CS_Low;
SPI_Send(0xAD);
High_Byte = GroundSpeed >> 24;
Mid_Byte1 = GroundSpeed >> 16;
SPI_Send(High_Byte);
SPI_Send(Mid_Byte1);
CS_High;
SPI_Wait(0x42);
CS_Low;
SPI_Send(0xAD);
Mid_Byte2 = GroundSpeed >> 8;
Low_Byte = GroundSpeed;
SPI_Send(Mid_Byte2);
SPI_Send(Low_Byte);
CS_High;
SPI_Wait(0x42);
//Continue auto address increment and write fix
CS_Low;
SPI_Send(0xAD);
SPI_Send(0x00);
SPI_Send(Fix);
CS_High;
SPI_Wait(0x42);
//Continue auto address increment and write latitude
CS_Low;
SPI_Send(0xAD);
High_Byte = Latitude >> 24;
Mid_Byte1 = Latitude >> 16;
SPI_Send(High_Byte);
SPI_Send(Mid_Byte1);
CS_High;
SPI_Wait(0x42);
CS_Low;
SPI_Send(0xAD);
Mid_Byte2 = Latitude >> 8;
Low_Byte = Latitude;
SPI_Send(Mid_Byte2);
SPI_Send(Low_Byte);
CS_High;
SPI_Wait(0x42);
//Continue auto address increment and write longitude
CS_Low;
SPI_Send(0xAD);
High_Byte = Longitude >> 24;
Mid_Byte1 = Longitude >> 16;
SPI_Send(High_Byte);
SPI_Send(Mid_Byte1);
CS_High;
SPI_Wait(0x42);
CS_Low;
SPI_Send(0xAD);
Mid_Byte2 = Longitude >> 8;
Low_Byte = Longitude;
SPI_Send(Mid_Byte2);
SPI_Send(Low_Byte);
CS_High;
SPI_Wait(0x42);
//Continue auto adresss increment and write date
CS_Low;
SPI_Send(0xAD);
High_Byte = Date >> 24;
Mid_Byte1 = Date >> 16;
SPI_Send(High_Byte);
SPI_Send(Mid_Byte1);
CS_High;
SPI_Wait(0x42);
CS_Low;
SPI_Send(0xAD);
Mid_Byte2 = Date >> 8;
Low_Byte = Date;
SPI_Send(Mid_Byte2);
SPI_Send(Low_Byte);
CS_High;
SPI_Wait(0x42);
//Continue auto address increment and write time
CS_Low;
SPI_Send(0xAD);
High_Byte = Time >> 24;
Mid_Byte1 = Time >> 16;
SPI_Send(High_Byte);
SPI_Send(Mid_Byte1);
CS_High;
SPI_Wait(0x42);
CS_Low;
SPI_Send(0xAD);
Mid_Byte2 = Time >> 8;
Low_Byte = Time;
SPI_Send(Mid_Byte2);
SPI_Send(Low_Byte);
CS_High;
SPI_Wait(0x42);
//Continue auto address increment and write lap time
CS_Low;
SPI_Send(0xAD);
High_Byte = LapTime >> 24;
Mid_Byte1 = LapTime >> 16;
SPI_Send(High_Byte);
SPI_Send(Mid_Byte1);
CS_High;
SPI_Wait(0x42);
CS_Low;
SPI_Send(0xAD);
Mid_Byte2 = LapTime >> 8;
Low_Byte = LapTime;
SPI_Send(Mid_Byte2);
SPI_Send(Low_Byte);
CS_High;
SPI_Wait(0x42);
//Disable write
CS_Low;
SPI_Send(0x04);
CS_High;
SPI_Wait(0x00);
//Update address value and number of records
SaveAddressValue = SaveAddressValue + 30;
Records = Records + 1;
}//Complete If Loop
}//Save While Loop
/*Store number of records in memory*/
//Write Enable
CS_Low;
SPI_Send(0x06); //write enable
CS_High;
//initiate auto adress increment and write Records
CS_Low;
SPI_Send(0xAD);
SPI_Send(0x00);
SPI_Send(0x00);
SPI_Send(0x02);
High_Byte = Records >> 8;
Low_Byte = Records;
SPI_Send(High_Byte);
SPI_Send(Low_Byte);
CS_High;
SPI_Wait(0x42);
//Disable write
CS_Low;
SPI_Send(0x04);
CS_High;
SPI_Wait(0x00);
}//Save If Loop
if(Download){
PutUART6('H');
asm("di");
/*Read number of records to dosnload from memory*/
DownloadAddressValue = 0x02;
CS_Low;
SPI_Send(0x03);
SPI_Send(DownloadAddressValue >> 16);
SPI_Send(DownloadAddressValue >> 8);
SPI_Send(DownloadAddressValue);
Records_High = SPI_Send(0x00);
Records_High = SPI_Send(0x00);
Records_Low = SPI_Send(0x00);
CS_High;
/*Send number of records to pc*/
PutUART6(Records_High);
PutUART6(Records_Low);
/*Get full value of records for For loop*/
Records = (Records | (unsigned char) Records_High) << 8;
Records = Records | Records_Low;
DownloadAddressValue = 0x05;
/*Begin Download*/
while(Download){
for(i = 0; i<Records;i=i+1){
/*Initialize Variables*/
RPM1 = 0;
Temperature = 0;
GroundSpeed = 0;
Fix = 0;
Latitude = 0;
Longitude = 0;
Date = 0;
Time = 0;
LapTime = 0;
/*retrieve 1 record's worth of data bits*/
CS_Low;
SPI_Send(0x0B);
SPI_Send(DownloadAddressValue >> 16);
SPI_Send(DownloadAddressValue >> 8);
SPI_Send(DownloadAddressValue);
RPM1_High_Byte = SPI_Send(0x00);
RPM1_High_Byte = SPI_Send(0x00);
RPM1_Low_Byte = SPI_Send(0x00);
Temp_High_Byte = SPI_Send(0x00);
Temp_Low_Byte = SPI_Send(0x00);
Speed_High_Byte = SPI_Send(0x00);
Speed_Mid_Byte1 = SPI_Send(0x00);
Speed_Mid_Byte2 = SPI_Send(0x00);
Speed_Low_Byte = SPI_Send(0x00);
Fix = SPI_Send(0x00);
Fix = SPI_Send(0x00);
Lat_High_Byte = SPI_Send(0x00);
Lat_Mid_Byte1 = SPI_Send(0x00);
Lat_Mid_Byte2 = SPI_Send(0x00);
Lat_Low_Byte = SPI_Send(0x00);
Long_High_Byte = SPI_Send(0x00);
Long_Mid_Byte1 = SPI_Send(0x00);
Long_Mid_Byte2 = SPI_Send(0x00);
Long_Low_Byte = SPI_Send(0x00);
Date_High_Byte = SPI_Send(0x00);
Date_Mid_Byte1 = SPI_Send(0x00);
Date_Mid_Byte2 = SPI_Send(0x00);
Date_Low_Byte = SPI_Send(0x00);
Time_High_Byte = SPI_Send(0x00);
Time_Mid_Byte1 = SPI_Send(0x00);
Time_Mid_Byte2 = SPI_Send(0x00);
Time_Low_Byte = SPI_Send(0x00);
LapTime_High_Byte = SPI_Send(0x00);
LapTime_Mid_Byte1 = SPI_Send(0x00);
LapTime_Mid_Byte2 = SPI_Send(0x00);
LapTime_Low_Byte = SPI_Send(0x00);
CS_High;
/*send RPM*/
PutUART6(RPM1_High_Byte);
PutUART6(RPM1_Low_Byte);
/*send temperature*/
PutUART6(Temp_High_Byte);
PutUART6(Temp_Low_Byte);
/*send speed*/
PutUART6(Speed_High_Byte);
PutUART6(Speed_Mid_Byte1);
PutUART6(Speed_Mid_Byte2);
PutUART6(Speed_Low_Byte);
/*Send fix */
PutUART6(Fix);
/*send latitude*/
PutUART6(Lat_High_Byte);
PutUART6(Lat_Mid_Byte1);
PutUART6(Lat_Mid_Byte2);
PutUART6(Lat_Low_Byte);
/*send longitude*/
PutUART6(Long_High_Byte);
PutUART6(Long_Mid_Byte1);
PutUART6(Long_Mid_Byte2);
PutUART6(Long_Low_Byte);
/*send date*/
PutUART6(Date_High_Byte);
PutUART6(Date_Mid_Byte1);
PutUART6(Date_Mid_Byte2);
PutUART6(Date_Low_Byte);
/*send time*/
PutUART6(Time_High_Byte);
PutUART6(Time_Mid_Byte1);
PutUART6(Time_Mid_Byte2);
PutUART6(Time_Low_Byte);
/*send lap time*/
PutUART6(LapTime_High_Byte);
PutUART6(LapTime_Mid_Byte1);
PutUART6(LapTime_Mid_Byte2);
PutUART6(LapTime_Low_Byte);
/*update new download address*/
DownloadAddressValue = DownloadAddressValue + 30;
}//Records For Loop
Download = 0;
asm("ei");
}//Download While Loop
}//Download If Loop
}//Main While Loop
return (EXIT_SUCCESS);
}
void Initialize_UART1(unsigned long rate){ //function that is the functional equivalent of serial_init()
U1STA = 0;
U1MODE = 0;
U1TXREG = 0;
U1RXREG = 0;
unsigned long FPBclock;
FPBclock = 10000000; //get the periferial bus clock rate
U1BRG = floor((FPBclock/(4*rate))-1); // calculate U1BRG
U1MODEbits.BRGH = 1; //High frequency
U1STAbits.URXEN = 1; //Enable recieve bit
U1STAbits.UTXEN = 1; //Enable transmit bit
U1MODEbits.ON = 1; //Enable UART
}
unsigned char GetUART1(void){
char Letter;
while(!U1STAbits.URXDA);
Letter = U1RXREG;
return Letter;
}
void PutUART1(unsigned char Value){
while(U1STAbits.UTXBF);
U1TXREG = Value;
}
void Send_Command (unsigned char* GPS_Signal, int Length)
{
int Ctr;
int Strlen = Length;//sizeof(gps_signal);
for(Ctr = 0; Ctr < Strlen; Ctr++){
PutUART1(GPS_Signal[Ctr]);
}
}
unsigned long Assign(unsigned int Index)
{
unsigned long Variable;
Variable = Sentence[Index];
Variable = (Variable << 8) + Sentence[Index-1];
Variable = (Variable << 8) + Sentence[Index-2];
Variable = (Variable << 8) + Sentence[Index-3];
return Variable;
}
void Initialize_A2D(void){
AD1PCFG = 0x05; //Make B4 analog
TRISBbits.TRISB4 = 1; //set B4 to input
AD1CON1bits.FORM = 0 ; //output is unsigned integer
AD1CON3bits.ADCS = 3; //select AN4 as channel
AD1CON1bits.ON = 1;
}
unsigned short A2D_Convert(void){
unsigned short Value = 0;
unsigned short Scaled_Value = 0;
AD1CHSbits.CH0SA = 4; //Select channel
AD1CON1bits.SAMP = 1; //Initiatie sampling
delay_us(1);
AD1CON1bits.SAMP = 0; //Terminate sampling
while(!AD1CON1bits.DONE);
Value = ADC1BUF0;
Scaled_Value = Value * (3300/1023);
Scaled_Value = (((Scaled_Value - 500)/10))*9/5 + 37;
return Scaled_Value;
}
void Initialize_Timer1(void){
T1CONbits.ON = 0;
T1CONbits.TCS = 1;
T1CONbits.TSYNC = 0;
T1CONbits.TCKPS = 0;
T1CONbits.TWDIS = 0;
TMR1 = 0x00;
PR1 = 65000;
TRISCbits.TRISC14 = 1; //set pin for sensor as input
T1CONbits.ON = 1;
}
void serialinitLCD(unsigned long baudrate){
U4MODE=0;
U4STA=0;
U4MODEbits.ON=1;
U4STAbits.UTXEN=1;
U4STAbits.URXISEL=0b00;
U4STAbits.URXEN=1;
U4MODEbits.BRGH=1;
unsigned long pbclk=10000000;
U4BRG=(pbclk/(4*baudrate) - 1);
//INTCONbits.MVEC=1; //Enable multivector interrupts
//asm("ei"); //Enable interrupts globally
return;
}
unsigned char setLCDvalue(char device[], unsigned char value){
unsigned char CMD=WRITE_OBJ, LOC1, LOC2, MSB, LSB, CKSUM;
LSB = value; //(0xFF) & (value);
MSB = 0x00;//(0xFF) & (value >> 8);
if (strcmp(device,"RPM")==0){
LOC1 = Coolgauge;
LOC2=0;
CKSUM = CMD ^ LOC1 ^ LOC2 ^ MSB ^ LSB;
putLCD(CMD);
putLCD(LOC1);
putLCD(LOC2);
putLCD(MSB);
putLCD(LSB);
putLCD(CKSUM);
LOC2=2;
CKSUM = CMD ^ LOC1 ^ LOC2 ^ MSB ^ LSB;
putLCD(CMD);
putLCD(LOC1);
putLCD(LOC2);
putLCD(MSB);
putLCD(LSB);
putLCD(CKSUM);
} else if (strcmp(device,"MPH")==0){
LOC1 = Coolgauge;
LOC2=1;
CKSUM = CMD ^ LOC1 ^ LOC2 ^ MSB ^ LSB;
putLCD(CMD);
putLCD(LOC1);
putLCD(LOC2);
putLCD(MSB);
putLCD(LSB);
putLCD(CKSUM);
LOC2=4;
CKSUM = CMD ^ LOC1 ^ LOC2 ^ MSB ^ LSB;
putLCD(CMD);
putLCD(LOC1);
putLCD(LOC2);
putLCD(MSB);
putLCD(LSB);
putLCD(CKSUM);
} else if (strcmp(device,"TEMP")==0){
LOC1 = Thermometer;
LOC2 = 0;
CKSUM = CMD ^ LOC1 ^ LOC2 ^ MSB ^ LSB;
putLCD(CMD);
putLCD(LOC1);
putLCD(LOC2);
putLCD(MSB);
putLCD(LSB);
putLCD(CKSUM);
}
return ACK;
}
unsigned char setLCDtext(char device[], char text[]){
unsigned char CMD=WRITE_STR, STR_INDEX, STR_LENGTH, CKSUM;
int i;
if (strcmp(device, "Download")==0) STR_INDEX = 0;
else if (strcmp(device,"Time")==0) STR_INDEX = 1;
STR_LENGTH = (unsigned char)strlen(text);
CKSUM = CMD ^ STR_INDEX ^ STR_LENGTH;
putLCD(CMD);
putLCD(STR_INDEX);
putLCD(STR_LENGTH);
for (i=0; i<strlen(text); i++){
putLCD(text[i]);
CKSUM = CKSUM ^ text[i];
}
putLCD(CKSUM);
return ACK;
}
void putLCD(unsigned char input){
while (U4STAbits.UTXBF==1);
U4TXREG=input;
return;
}
unsigned char getLCD(void){
while (U4STAbits.URXDA==0);
return U4RXREG;
}
void Initialize_SPI4(void){
int ClearedData;
AD1PCFGbits.PCFG8 = 1; //set signal pin to digital I/O
DDPCONbits.JTAGEN = 0; //disable JTAG
ClearedData = SPI4BUF; //clear receive buffer
SPI4CON = 0; //reset spi module
SPI4CONbits.CKE = 0; //set clock edge
SPI4CONbits.CKP = 1; //set clock polarity
SPI4CONbits.SMP = 1; //set sample phase
SPI4CONbits.MSTEN = 1; //enable master mode
SPI4STATbits.SPIROV = 0; //clear overflow bit
SPI4BRG = 0; //set baude rate (Fsck = 5 MHz, Fpb = 10MHz)
TRISBbits.TRISB8 = 0; //set CS pin as output
CS_High;
SPI4CONbits.ON = 1; //enable SPI
}
unsigned char SPI_Send(unsigned char Command){
unsigned char Receive_Data;
SPI4BUF = Command;
while(!IFS1bits.SPI4RXIF); //wait until complete
IFS1bits.SPI4RXIF = 0; //clear interrupt flag
Receive_Data = SPI4BUF;
return Receive_Data;
}
void Chip_Setup(void){
unsigned char SPI_Read = 0;
//disable any previous AAI write
CS_Low;
SPI_Send(0x04);
CS_High;
//Read Chip ID
CS_Low;
SPI_Send(0xAB);
SPI_Send(0x00);
SPI_Send(0x00);
SPI_Send(0x00);
SPI_Read = SPI_Send(0x00);
SPI_Read = SPI_Send(0x00);
CS_High;
//Make Chip Writeable
CS_Low;
SPI_Send(0x50); //enable register writing
CS_High;
CS_Low;
SPI_Send(0x01); //write to register
SPI_Send(0x00); //unprotect all memory area
CS_High;
CS_Low;
SPI_Send(0x05); //read register
SPI_Read = SPI_Send(0x00); //Read response
CS_High;
}
void Chip_Clear(void){
unsigned char SPI_Read = 0;
//Clear Chip
CS_Low;
SPI_Send(0x06); //write enable
CS_High;
CS_Low;
SPI_Send(0x60); //clear chip
CS_High;
}
void SPI_Wait(unsigned char Command){
unsigned char SPI_Read = 0;
CS_Low;
SPI_Send(0x05);
SPI_Read = SPI_Send(0x00);
SPI_Read = SPI_Send(0x00);
while(SPI_Read != Command){
SPI_Read = SPI_Send(0x00);
}
CS_High;
}
void Initialize_UART6(unsigned long Rate){ //function that is the functional equivalent of serial_init()
U6STA = 0;
U6MODE = 0;
U6TXREG = 0;
U6RXREG = 0;
unsigned long FPBclock;
FPBclock = 10000000;
U6BRG = floor((FPBclock/(4*Rate)-1)); // calculate U6BRG
U6MODEbits.ON = 1; //Enable UART
U6MODEbits.BRGH = 1; //High frequency
U6STAbits.URXEN = 1; //Enable recieve bit
U6STAbits.UTXEN = 1; //Enable transmit bit
}
void PutUART6(unsigned char Value){
while(U6STAbits.UTXBF);
U6TXREG = Value;
}
unsigned char GetUART6(void){
unsigned char Received_Data;
while(!U6STAbits.URXDA); //wait until register is filled
Received_Data = U6RXREG; //grab data
return Received_Data;
}
Appendix F: Configuration Bits Header File
/*
* File: configbits.h
* Author: Mike
*
* Created on October 9, 2012, 1:50 PM
*/
#ifndef CONFIGBITS_H
#define CONFIGBITS_H
/*
* REv 8 boards.
* resonator is 8 MHz
* Will switch to internal if external not present or fails
* internal (FRC) clock
peripher clock = at 10 MHz (80 MHz/8)
*/
#pragma config FNOSC = PRIPLL // Oscillator selection
#pragma config POSCMOD = HS // Primary oscillator mode
#pragma config FPLLIDIV = DIV_5 // PLL input divider (20 -> 4)
#pragma config FPLLMUL = MUL_20 // PLL multiplier ( 4x20 = 80)
#pragma config FPLLODIV = DIV_1 // PLL output divider
#pragma config FPBDIV = DIV_8 // Peripheral bus clock divider 10 mhz
#pragma config FSOSCEN = OFF // Secondary oscillator enable
/* Clock control settings
*/
#pragma config IESO = ON // Internal/external clock switchover
#pragma config FCKSM = CSECME // Clock switching (CSx)/Clock monitor (CMx)
#pragma config OSCIOFNC = OFF // Clock output on OSCO pin enable
/* USB Settings
*/
#pragma config UPLLEN = OFF // USB PLL enable
#pragma config UPLLIDIV = DIV_2 // USB PLL input divider
#pragma config FVBUSONIO = OFF // VBUS pin control
#pragma config FUSBIDIO = OFF // USBID pin control
/* Other Peripheral Device settings
*/
#pragma config FWDTEN = OFF // Watchdog timer enable
#pragma config WDTPS = PS1024 // Watchdog timer post-scaler
#pragma config FSRSSEL = PRIORITY_7 // SRS interrupt priority
#pragma config DEBUG = ON
#pragma config ICESEL = ICS_PGx1 // ICE pin selection
#endif /* CONFIGBITS_H */
Appendix G: LCD Screen Touch IDS
/*
* File: TouchScreenIDs.h
* Author: Eric
*
* Created on February 25, 2015, 3:02 PM
*/
#ifndef TOUCHSCREENIDS_H
#define TOUCHSCREENIDS_H
//Commands
#define READ_OBJ 0x00
#define WRITE_OBJ 0x01
#define WRITE_STR 0x02
#define WRITE_STRU 0x03
#define WRITE_CONTRAST 0x04
#define REPORT_OBJ 0x05
#define REPORT_EVENT 0x07
//Acks
#define LCD_ACK 0x06
#define LCD_NAK 0x15
//Object IDs
#define Dipswitch 0
#define Knob 1
#define Rockerswitch 2
#define Rotaryswitch 3
#define Slider 4
#define Trackbar 5
#define Winbutton 6
#define Angularmeter 7
#define Coolgauge 8
#define Customdigits 9
#define Form 10 //Used to set the current form
#define Gauge 11
#define Image 12 //Displayed as part of form, no method to alter
#define Keyboard 13 //Keyboard inputs are always single bytes and are unsolicited
#define Led 14
#define Leddigits 15
#define Meter 16
#define Strings 17
#define Thermometer 18
#define Userled 19
#define Video 20
#define Statictext 21 //Displayed as part of form, no method to alter
#define Sound 22
#define Timer 23
#define Spectrum 24
#define Scope 25
#define Tank 26
#define UserImages 27
#define PinOutput 28
#define PinInput 29
#define Button4D 30
#define AniButton 31
#define ColorPicker 32
#define UserButton 33
#endif /*TOUCHSCREENIDS_H */
This Web Page was Built with PageBreeze Free HTML Editor