This is used for an RC racing light tree. It stays red until you push the button, then stays yellow for 1-3 seconds (randomly), then flashes all colors twice, then stays green until you push the button. Weak pull-ups are enabled on pin 5 for the button.
/*
* File: RaceLightsMain.c
* Author: hmikelson
*
* Created on June 14, 2019, 1:26 PM
*/
#if defined(__XC)
#include <xc.h> /* XC8 General Include File */
#elif defined(HI_TECH_C)
#include <htc.h> /* HiTech General Include File */
#endif
#include <stdint.h> /* For uint8_t definition */
#include <stdbool.h> /* For true/false definition */
#include <stdlib.h> /*rand()*/
#pragma config MCLRE=OFF,CP=OFF,WDTE=OFF,FOSC=INTOSCIO
#define _XTAL_FREQ 4000000
uint8_t sGPIO;
void init()
{
//Configure GPIO Port
ANSEL = 0b00000000; //Configure all GPIO pins as digital
TRISIO = 0b11101000; //Set GP3 & 5 as input and the rest as outputs
OPTION_REGbits.nGPPU = 0;
WPU = 0b00100000; //Enable weak pullups on GP5
//Configuer AD Convertor
ADCON0 = 0x00; //AD disabled
ADRESH = 0x00; //Init the AD Register
//Configure Comparator
CMCON0 = 0xFF; // Comparator is turned off
CMCON1 = 0x00; // Comparator is turned off
//Interrupt configuration
INTCON = 0x00; //Disable all interrupts
}
void vdelay(int n)
{
int i;
for (i=0;i<=n;i++)
{
__delay_ms(150);
}
}
void main()
{
uint8_t r, db_cnt;
init();
GPIO = 0b00000001;
__delay_ms(100);
GPIO = 0b00000010;
__delay_ms(100);
GPIO = 0b00000100;
__delay_ms(100);
GPIO = 0b00000000;
while(1)
{
GPIO = 0b00000001;
__delay_ms(200);
for (db_cnt = 0; db_cnt <= 10; db_cnt++)
{
__delay_ms(10);
if (GPIObits.GP5 == 1)
db_cnt = 0;
}
GPIO = 0b00000010;
r = rand()%15;
__delay_ms(250);
vdelay(r);
__delay_ms(250);
GPIO = 0b00000001;
__delay_ms(50);
GPIO = 0b00000010;
__delay_ms(50);
GPIO = 0b00000100;
__delay_ms(50);
GPIO = 0b00000001;
__delay_ms(50);
GPIO = 0b00000010;
__delay_ms(50);
GPIO = 0b00000100;
__delay_ms(50);
GPIO = 0b00000100;
__delay_ms(200);
for (db_cnt = 0; db_cnt <= 10; db_cnt++)
{
__delay_ms(10);
if (GPIObits.GP5 == 1)
db_cnt = 0;
}
}
}
No comments:
Post a Comment