Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
h>
#include <string.h>
// FICD
#pragma config ICS = PGD3
// ICD Communication Channel Select bits (Communicate on PGEC1 and PGED1)
#pragma config JTAGEN = OFF
// JTAG Enable bit (JTAG is disabled)
// FPOR
#pragma config ALTI2C1 = OFF
// Alternate I2C1 pins (I2C1 mapped to SDA1/SCL1 pins)
#pragma config ALTI2C2 = OFF
// Alternate I2C2 pins (I2C2 mapped to SDA2/SCL2 pins)
#pragma config WDTWIN = WIN25
// Watchdog Window Select bits (WDT Window is 25% of WDT period)
// FWDT
#pragma config WDTPOST = PS32768
// Watchdog Timer Postscaler bits (1:32,768)
#pragma config WDTPRE = PR128
// Watchdog Timer Prescaler bit (1:128)
#pragma config PLLKEN = ON
// PLL Lock Enable bit (Clock switch to PLL source will wait until the PLL lock
signal is valid.)
#pragma config WINDIS = OFF
// Watchdog Timer Window Enable bit (Watchdog Timer in Non-Window mode)
#pragma config FWDTEN = OFF
// Watchdog Timer Enable bit (Watchdog timer enabled/disabled by user software)
// FOSC
#pragma config POSCMD = HS
// Primary Oscillator Mode Select bits (HS Crystal Oscillator Mode)
#pragma config OSCIOFNC = OFF
// OSC2 Pin Function bit (OSC2 is clock output)
#pragma config IOL1WAY = OFF
// Peripheral pin select configuration (Allow multiple reconfigurations)
#pragma config FCKSM = CSECME
// Clock Switching Mode bits (Both Clock switching and Fail-safe Clock Monitor a
re enabled)
// FOSCSEL
#pragma config FNOSC = PRIPLL
// Oscillator Source Selection (Primary Oscillator with PLL module (XT + PLL, HS
+ PLL, EC + PLL))
#pragma config PWMLOCK = ON
// PWM Lock Enable bit (Certain PWM registers may only be written after key sequ
ence)
#pragma config IESO = ON
// Two-speed Oscillator Start-up Enable bit (Start up device with FRC, then swit
ch to user-selected oscillator source)
// FGS
#pragma config GWRP = OFF
// General Segment Write-Protect bit (General Segment may be written)
#pragma config GCP = OFF
// General Segment Code-Protect bit (General Segment Code protect is Disabled)
/**** TMR1 Variables ****/
volatile unsigned long int cnt = 0;
in for U1STA
= 0; //Bit15
0; //Bit14
= 0; //Bit13
SFR
Int when Char is transferred (1/2 config!)
N/A, IRDA config
Other half of Bit15
//U1STAbits.notimplemented = 0;//Bit12
U1STAbits.UTXBRK = 0; //Bit11 Disabled
U1STAbits.UTXEN = 0;
//Bit10 TX pins controlled by periph
U1STAbits.UTXBF = 0;
//Bit9 *Read Only Bit*
U1STAbits.TRMT = 0;
//Bit8 *Read Only bit*
U1STAbits.URXISEL = 0; //Bits6,7 Int. on character recieved
U1STAbits.ADDEN = 0;
//Bit5 Address Detect Disabled
U1STAbits.RIDLE = 0;
//Bit4 *Read Only Bit*
U1STAbits.PERR = 0;
//Bit3 *Read Only Bit*
U1STAbits.FERR = 0;
//Bit2 *Read Only Bit*
U1STAbits.OERR = 0;
//Bit1 *Read Only Bit*
U1STAbits.URXDA = 0;
//Bit0 *Read Only Bit*
IPC7 = 0x4400;
// Mid Range Interrupt Priority level, no urgent rea
son
//IFS0bits.U1TXIF
//IEC0bits.U1TXIE
IFS0bits.U1RXIF =
IEC0bits.U1RXIE =
//IPC3bits.U1TXIP
IPC2bits.U1RXIP =
RPOR1bits.RP37R =
RPINR18bits.U1RXR
U1MODEbits.UARTEN
U1STAbits.UTXEN =
= 0;
// Clear the Transmit Interrupt Flag
= 1;
// Enable Transmit Interrupts
0;
// Clear the Recieve Interrupt Flag
1;
// Enable Recieve Interrupts
= 0x03;
0x01;
1;
//RP37/RB5 as U1TX
= 38; //RP38/RB6 as U1RX
= 1; // And turn the peripheral on
1;
}
void printUART1(char *data) {
while(*data) {
U1TXREG = *data++;
while(U1STAbits.UTXBF); // wait for TX buffer to clear
}
}
void __attribute__((interrupt, auto_psv)) _T1Interrupt(void) {
cnt++;
if(led1++ >= 10000) {
LATAbits.LATA1 ^= 1;
led1 = 0;
}
if(led_overrun) {
LATBbits.LATB0 ^= 1;
led_overrun = 0;
}
IFS0bits.T1IF = 0;
}
// #2 priority
void __attribute__((interrupt, no_auto_psv)) _U1RXInterrupt(void) {
// do something here when UART1 receives (the GPS)
if(newgps == 0) {
gpsdata[gpspos] = U1RXREG;
if(gpspos == 5 && (gpsdata[3] != gps_str_request[0] ||
gpsdata[4] != gps_str_request[1] ||
gpsdata[5] != gps_str_request[2])) {
// We don't have a GPRMC string, discard!
gpsdata[0] = 0;
gpspos = 0;
newgps = 0;
}
if(gpspos == 0 && gpsdata[gpspos] != '$') {
//We haven't received the start
//character, so ignore the data.
gpsdata[0] = 0;
gpspos = 0;
newgps = 0;
}
else if(gpspos >= 79) {
for(gpspos = 0; gpspos < 80; gpspos++) gpsdata[gpspos] = 0;
gpspos = 0;
newgps = 0;
}
else if(gpspos != 0 && (gpsdata[gpspos] == '\n' || gpsdata[gpspos] == '$
')) {
//We've reached the end of a
//new string, so stop receiving
//or else the new string will
//overwrite the old one.
int tmp;
if(gpsdata[gpspos] == '\n') for(tmp = gpspos + 1; tmp < 80; tmp++) g
psdata[tmp] = 0;
else for(tmp = gpspos; tmp < 80; tmp++) gpsdata[tmp] = 0;
gpspos = 0;
newgps = 1;
}
else {
gpspos++;
newgps = 0;
}
}
IFS0bits.U1RXIF = 0;
}
// #1 priority
//
//
//
//
//
int main(void) {
ANSELA = 0x0000;
ANSELB = 0x0000;
// configure all pins as digital
TRISAbits.TRISA1 = 0;
TRISBbits.TRISB0 = 0;
clk = initPLL(0, 0, 68, 8);
// so the chip knows how fast the PLL is actually running at
initTMR1(0b01, clk / 2);
// cycling at 17.5 MHz / 70 = 250000 Hz
initUART1();
// initialize UART1
printUART1("Start of Program!\n");
strcpy(gps_str_request, "GGA");
while(1) {
if(U1STAbits.OERR) {
// if there's an overrun error
led_overrun = 1;
U1STAbits.OERR = 0;
while(1) LATBbits.LATB0 = 1;
}
if(newgps == 1) {
newgps = 0;
printUART1(gpsdata); // it's a GPRMC string
}
}
}