SpeedyCat.C | Embedded Systems NDSU with Ethan Zeltinger

Описание к видео SpeedyCat.C | Embedded Systems NDSU with Ethan Zeltinger

This is our Term Project for ECE 376. We used C-Code and a PIC microcontroller in this project.

C-Code
/**
* @file SpeedyCat.c
* @author Ethan Zeltinger, Joran Oien
* @brief Measures the speed of a cat (or anything) with an ultrasonic sensor
* @date 05-7-2021
*
*/
#include (less than angle bracket)pic18.h(greater than angle bracket)
#include "lcd_portd.c"

// Global Variables
unsigned long int TIME, TIME0, TIME1, dT;
const unsigned int Y = 15625;

const unsigned char MSG0[20] = "SpeedyCat.C ";
const unsigned char MPH[4] = "MPH";
const unsigned char MPS[4] = "M/S";

// Interrupt Service Routine
void interrupt IntServe(void)
{
if (TMR0IF) {
TMR0 = -Y;
RC0 = !RC0;
TMR0IF = 0;
}
if (TMR1IF) {
TIME = TIME + 0x10000;
TMR1IF = 0;
}
if (CCP1IF) {
if (CCP1CON == 0x05) { // rising edge
TIME0 = TIME + CCPR1;
CCP1CON = 0x04;
}
else {
TIME1 = TIME + CCPR1;
dT = TIME1 - TIME0;
CCP1CON = 0x05;
}
CCP1IF = 0;
}
}

void main(void)
{
float mm, meters, meters_per_second, milesPerHour, lastValue, meterPerSec, topMeterPerSec;
TRISA = 0;
TRISB = 0xFF;
TRISC = 0x04; // capture every rising edge
TRISD = 0;
ADCON1 = 0x0F;
LCD_Init();
LCD_Move(0,0); for (int i=0; i(less than angle bracket)20; i++) LCD_Write(MSG0[i]);
Wait_ms(1000);
LCD_Inst(1);
TIME = 0;
// set up Timer0 for PS = 4
T0CS = 0;
T0CON = 0x85;
TMR0ON = 1;
TMR0IE = 1;
TMR0IP = 1;
PEIE = 1;
// set up Timer1 for PS = 8
TMR1CS = 0;
T1CON = 0x81;
TMR1ON = 1;
TMR1IE = 1;
TMR1IP = 1;
PEIE = 1;
// set up Capture1 for rising edges
TRISC2 = 1;
CCP1CON = 0x05;
CCP1IE = 1;
PEIE = 1;
// Turn on the serial port for 9600 baud
TRISC = TRISC | 0xC0;
TXIE = 0;
RCIE = 0;
BRGH = 1;
BRG16 = 1;
SYNC = 0;
SPBRG = 255;
TXSTA = 0x22;
RCSTA = 0x90;

// turn on all interrupts
GIE = 1;

lastValue, topMeterPerSec = 0;
LCD_Move(0,12); for (int i=0; i(less than angle bracket)3; i++) LCD_Write(MPH[i]);
LCD_Move(1,12); for (int i=0; i(less than angle bracket)3; i++) LCD_Write(MPS[i]);

while(1) {
if(RB0){
// Need to measure distances periodically and calculate speed
// (distance2 - distance1) * 1.0 / (t2 - t1) * scale_meters_per_second
mm = dT * 01.715; // 1 count = 1/100 mm
meters = mm/1000;
// meters_per_second = meters / seconds;
meterPerSec = (meters - lastValue)*10;
if(meterPerSec (greater than angle bracket) topMeterPerSec && meterPerSec (less than angle bracket) 1000)
topMeterPerSec = meterPerSec;
if(lastValue != meters){
SCI_Out(meterPerSec, 5, 2);
SCI_CRLF();
}
lastValue = meters;
}
else if(RB7){
topMeterPerSec = 0;
}
LCD_Move(0,0); LCD_Out(topMeterPerSec, 5, 2);
milesPerHour = topMeterPerSec * 2.23694;
LCD_Move(1,0); LCD_Out(milesPerHour, 5, 2);

}
}

Комментарии

Информация по комментариям в разработке