Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /***** Project Header *****/
- // Project Name:
- // Author:
- // Date: 25/10/2016 8:39
- // Code auto-generated by systemdesignerjs
- // from www.techideas.co.nz
- /***** Hardware defines *****/
- //make sure this matches your oscillator setting
- #define F_CPU 16000000//crystal
- /***** Includes *****/
- #include <avr/io.h>
- //#include <stdint.h>
- #include <util/delay.h>
- #include <avr/interrupt.h>
- //#include <avr/eeprom.h>
- //#include <stdio.h>
- //#include <string.h>
- //#include <avr/pgmspace.h>
- /***** Hardware macros *****/
- //Hardware macros for outputs
- #define SET_EW_RED PORTC |= (1<<PC3)
- #define CLR_EW_RED PORTC &= ~(1<<PC3)
- #define SET_EW_ORN PORTC |= (1<<PC4)
- #define CLR_EW_ORN PORTC &= ~(1<<PC4)
- #define SET_EW_GRN PORTC |= (1<<PC5)
- #define CLR_EW_GRN PORTC &= ~(1<<PC5)
- #define SET_NS_RED PORTD |= (1<<PD5)
- #define CLR_NS_RED PORTD &= ~(1<<PD5)
- #define SET_NS_ORN PORTB |= (1<<PB1)
- #define CLR_NS_ORN PORTB &= ~(1<<PB1)
- #define SET_NS_GRN PORTB |= (1<<PB0)
- #define CLR_NS_GRN PORTB &= ~(1<<PB0)
- #define SET_EW_DC PORTC |= (1<<PC0)
- #define CLR_EW_DC PORTC &= ~(1<<PC0)
- #define SET_EW_CROSS PORTC |= (1<<PC1)
- #define CLR_EW_CROSS PORTC &= ~(1<<PC1)
- #define SET_NS_DC PORTD |= (1<<PD7)
- #define CLR_NS_DC PORTD &= ~(1<<PD7)
- #define SET_NS_CROSS PORTD |= (1<<PD6)
- #define CLR_NS_CROSS PORTD &= ~(1<<PD6)
- //Hardware macros for inputs
- #define SW_PED1_IS_LOW ~PINB & (1<<PB5)
- #define SW_PED1_IS_HIGH PINB & (1<<PB5)
- #define SW_PED2_IS_LOW ~PINB & (1<<PB4)
- #define SW_PED2_IS_HIGH PINB & (1<<PB4)
- #define SW_PED3_IS_LOW ~PINB & (1<<PB3)
- #define SW_PED3_IS_HIGH PINB & (1<<PB3)
- #define SW_PED4_IS_LOW ~PINB & (1<<PB2)
- #define SW_PED4_IS_HIGH PINB & (1<<PB2)
- #define TIMING_TEST_SW_IS_LOW ~PIND & (1<<PD2)
- #define TIMING_TEST_SW_IS_HIGH PIND & (1<<PD2)
- //Hardware macros for ADC inputs
- /***** User macros *****/
- #define EW_TIME 210
- #define NS_TIME 190
- #define ALL_RED_TIME 25
- #define ORANGE_TIME 16
- #define PED_CROSS_TIME 5
- #define FLASH_AMOUNT 20
- #define FLASH_DELAY 500
- #define MS_INCREMENT_AMOUNT 10
- #define MS_IN_S 1000
- /***** Declare & initialise global variables *****/
- //DO YOU KNOW WHY THESE ARE VOLATILE??
- volatile uint16_t ms_count = 0;
- volatile uint16_t secs = 0;
- volatile uint8_t ped_activated = 0;
- /***** Interrupt Service Routines *****/
- ISR(INT0_vect){ //THIS IS SETUP SO THAT YOU CAN TEST YOUR CODE
- secs = 500;
- }
- ISR(TIMER0_COMPA_vect) { //called when the timer overflows
- ms_count = ms_count+MS_INCREMENT_AMOUNT;
- if (ms_count == MS_IN_S){
- ms_count = 0;
- secs++;
- }
- }
- /***** Prototypes for Functions *****/
- void poll_ped_sw(); //returns true if any switch is pressed
- void ew_go();
- void ew_orange();
- void ns_go();
- void ns_orange();
- void all_red();
- void peds_cross();
- void peds_dont_cross();
- void ped_lights_off();
- void flash_dont_cross();
- /***** Declare state var and constants *****/
- #define ST_EW 1
- #define ST_NS 2
- #define ST_NS_ORANGE 3
- #define ST_EW_ORANGE 4
- #define ST_PEDS_DONT_CROSS 5
- #define ST_PEDS_CROSS 6
- #define ST_ALL_RED 7
- uint8_t state = ST_EW;
- /***** Main function *****/
- int main(void) {
- /***** Initial hardware setups go here *****/
- /***** IO Hardware Config *****/
- // Initially make all micro pins outputs
- DDRB = 0xff; //set as outputs
- DDRC = 0xff; //set as outputs
- DDRD = 0xff; //set as outputs
- // make these pins inputs
- DDRB &= ~ (1 << PB5);
- DDRB &= ~ (1 << PB4);
- DDRB &= ~ (1 << PB3);
- DDRB &= ~ (1 << PB2);
- DDRD &= ~ (1 << PD2);
- PORTB |= (1 << PB5); //activate internal pullup R for SW_PED1
- PORTB |= (1 << PB4); //activate internal pullup R for SW_PED2
- PORTB |= (1 << PB3); //activate internal pullup R for SW_PED3
- PORTB |= (1 << PB2); //activate internal pullup R for SW_PED4
- PORTD |= (1 << PD2); //activate internal pullup R for TIMING_TEST_SW
- /***** Main variables go here *****/
- uint8_t last_state = 0;
- /***** Run once code goes here *****/
- EIMSK |= (1 << INT0); // INT0_ENABLE_
- EICRA |= (1 << ISC01); //falling edge
- // Timer Config setup Timer0 - 2mSec
- TCCR0A |= (1 << COM0A1);
- TCCR0A |= (1 << COM0A0);
- TCCR0B |= (1 << CS02);
- TCCR0B |= (1 << CS00);
- TCCR0A |= (1 << WGM01);
- TIMSK0 |= (1 << OCIE0A);
- OCR0A = 155;
- sei();
- /***** Pre-SM Actions *****/
- ew_go();
- /***** Loop code *****/
- while (1) {
- /***** Statemachine *****/
- /****** ST_EW ******/
- while (state == ST_EW) {
- poll_ped_sw();
- if (secs > EW_TIME) {
- state = ST_EW_ORANGE;
- ew_orange();
- secs = 0;
- ms_count = 0;
- }
- }
- /****** ST_NS ******/
- while (state == ST_NS) {
- poll_ped_sw();
- if (secs > EW_TIME) {
- state = ST_NS_ORANGE;
- ew_orage();
- secs = 0;
- ms_count = 0;
- }
- }
- /****** ST_NS_ORANGE ******/
- while (state == ST_NS_ORANGE) {
- poll_ped_sw();
- if (ped_activated && secs > ORANGE_TIME) {
- state = ST_ALL_RED;
- all_red();
- secs = 0;
- ms_count = 0;
- last_state = 0;
- }
- if (!ped_activated && secs > ORANGE_TIME) {
- state = ST_EW;
- ns_go();
- secs = 0;
- ms_count = 0;
- ;
- }
- }
- /****** ST_EW_ORANGE ******/
- while (state == ST_EW_ORANGE) {
- poll_ped_sw();
- if (!ped_activated && secs > ORANGE_TIME) {
- state = ST_NS;
- ns_go();
- secs = 0;
- ms_count = 0;
- }
- if (ped_activated && secs > ORANGE_TIME) {
- state = ST_ALL_RED;
- all_red();
- secs = 0;
- ms_count = 0;
- last_state = 1;
- }
- }
- /****** ST_PEDS_DONT_CROSS ******/
- while (state == ST_PEDS_DONT_CROSS) {
- flash_dont_cross();
- if (last_state == 1) {
- state = ST_NS;
- }
- if (last_state == 0) {
- state = ST_EW;
- }
- }
- /****** ST_PEDS_CROSS ******/
- while (state == ST_PEDS_CROSS) {
- if (secs > PED_CROSS_TIME) {
- state = ST_PEDS_DONT_CROSS;
- secs = 0;
- ms_count = 0;
- ;
- }
- }
- /****** ST_ALL_RED ******/
- while (state == ST_ALL_RED) {
- if (secs > 1) {
- state = ST_PEDS_CROSS;
- peds_cross();
- }
- }
- } //while end
- } //main end
- /***** Functions *****/
- void poll_ped_sw() {
- if (SW_PED3_IS_LOW | SW_PED2_IS_LOW | SW_PED1_IS_LOW | SW_PED4_IS_LOW) {
- ped_activated;
- peds_dont_cross();
- }
- }
- //IT IS USEFUL TO SETUP THE FULL CONDITIONS FOR EACH VEHICLE LIGHT IN EACH FUNCTION
- //THIS WAY WE AVOID ANY CONFUSION ABOUT WHAT LIGHTS SHOULD BE TURNED ON AND OFF
- void ew_go() {
- SET_EW_GRN;
- CLR_EW_ORN;
- CLR_EW_RED;
- CLR_NS_GRN;
- CLR_NS_ORN;
- SET_NS_RED;
- }
- void ew_orange() {
- CLR_EW_GRN;
- SET_EW_ORN;
- CLR_EW_RED;
- CLR_NS_GRN;
- CLR_NS_ORN;
- SET_NS_RED;
- }
- void ns_go() {
- CLR_EW_GRN;
- CLR_EW_ORN;
- SET_EW_RED;
- SET_NS_GRN;
- CLR_NS_ORN;
- CLR_NS_RED;
- }
- void ns_orange() {
- CLR_EW_GRN;
- CLR_EW_ORN;
- SET_EW_RED;
- CLR_NS_GRN;
- SET_NS_ORN;
- CLR_NS_RED;
- }
- //BEFORE THE PED CROSSES THERE IS A BRIEF PERIOD WHEN
- //WE WANT TO MAKE SURE THAT ALL RED LIGHTS ARE ON AND GRN & ORANGE ARE ON
- void all_red() {
- CLR_EW_GRN;
- CLR_EW_ORN;
- SET_EW_RED;
- SET_EW_DC;
- CLR_EW_CROSS;
- CLR_NS_GRN;
- CLR_NS_ORN;
- SET_NS_RED;
- SET_NS_DC;
- CLR_NS_CROSS;
- }
- //
- void peds_cross() {
- CLR_EW_GRN;
- CLR_EW_ORN;
- SET_EW_RED;
- CLR_EW_DC;
- SET_EW_CROSS;
- CLR_NS_GRN;
- CLR_NS_ORN;
- SET_NS_RED;
- CLR_NS_DC;
- SET_NS_CROSS;
- }
- void peds_dont_cross() {
- CLR_NS_CROSS;
- CLR_EW_CROSS;
- SET_NS_DC;
- SET_EW_DC;
- }
- void ped_lights_off() {
- CLR_NS_CROSS;
- CLR_EW_CROSS;
- CLR_NS_DC;
- CLR_EW_DC;
- }
- void flash_dont_cross() {
- CLR_NS_CROSS;
- CLR_EW_CROSS;
- for (int i = 0; i < FLASH_AMOUNT; i++) {
- CLR_NS_DC;
- CLR_EW_DC;
- while (ms_count < 500) {
- //blocking
- }
- SET_NS_DC;
- SET_EW_DC;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement