Skip to content

Instantly share code, notes, and snippets.

@JHeld07
Created December 16, 2015 19:56
Show Gist options
  • Save JHeld07/0b9f7636a0f8af9777c1 to your computer and use it in GitHub Desktop.
Save JHeld07/0b9f7636a0f8af9777c1 to your computer and use it in GitHub Desktop.
// Challenge 4 Microcontolers Fall 2015 Josh Held
#include <18f4520.h>
#use delay(clock=20000000)
#fuses HS, NOWDT, NOLVP
#include "../../Library/18f4520ptr.h"
#include "../../Library/modifiedlcd.h"
float incoming, outgoing, RPM;
#INT_RDA // Interrupt for recieve data Only for USART Receive interupt Triggered by proper USART packet receive
void int_rda_isr(){
incoming=*RCREG;
*TXREG=RPM;
}
// RCREG = Data Receiver Register
// TXREG = Data Trasmit Register
//Frequency counter
unsigned int OFCnt=0;
#INT_TIMER1 // Counter Overflows
void Int_timer1_isr(){
OFCnt++;
}
// Frequency counter
unsigned int32 tstart, tstop, dT;
#INT_CCP2
void int_ccp2_isr(){
tstop = *CCPR2; //Get the time off the system...
dT = (OFCnt*0x10000)-tstart+tstop;
tstart = tstop;
OFCnt = 0;
}
main(){
float time;
float x=50;
float CRPM, TRPM;
int y;
lcd_init();
ADCON1->PCFGx=0xF; // All Digital
*TRISC=0x82;
*TRISA=0x00; // output toggle setup
TXSTA->TXEN=1; // Tx On
TXSTA->SYNC=0; // Asynch
TXSTA->BRGH=0; // Low Speed
RCSTA->SPEN=1; // Rx On RCSTA is setting up two interupts.
RCSTA->CREN=1; // Rx Cont. on
BAUDCON->BRG16=1; // Setting baud rate based off of table.
*SPBRG=129; // setting the interupt
PIE1->RCIE=1; // Rx Interupt Enable
// If Tx Enable is ON it wont work.
INTCON->GIE=1;
INTCON->PEIE=1;
// Setup Overflow
T1CON->TMR1ON=1;
T1CON->TMR1CS=0;
T1CON->T1CKPSx=0; // Setting PS to 1
// Setup my Capture
CCP2CON->CCPxMx=4;
// Setup my Interrupts
PIE1->TMR1IE=1; // Overflow Interupt
PIE2->CCP2IE=1;
//PWM
T2CON->TMR2ON=1; // Problem line in challenge //PWM has to use timer 2.
CCP1CON->CCPxMx=0xC;
//*CCPR1=x;
*PR2=100;
INTCON->GIE=1;
INTCON->PEIE=1;
INTCON->INT0IE=1;
INTCON2->INTEDG0=1;
while(1){
//incoming=CurrentRPM;
TRPM=(117*(incoming/100));
CRPM=RPM;
if((TRPM-1)>=CRPM){
x=x+1;
}
else if ((TRPM+1)<=CRPM){
x=x-1;
}
//float RPM;
RPM=(1/((161*time)/60));
time=dT*0.2e-6; // PS 1 and Fosc 20m
printf(lcd_putc, "\f%2.2f T%2.2f", incoming, TRPM);
printf(lcd_putc,"\nRPM=%2.2f", RPM);
//*CCPR1=incoming; //working pwm driver
*CCPR1=x;
delay_ms(300);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment