Advertisement
Guest User

Untitled

a guest
May 25th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.71 KB | None | 0 0
  1. /***** Project Header *****/
  2. // Project Name:
  3. // Author:
  4. // Date: 25/10/2016 8:39
  5. // Code auto-generated by systemdesignerjs
  6. // from www.techideas.co.nz
  7.  
  8. /***** Hardware defines *****/
  9. //make sure this matches your oscillator setting
  10. #define F_CPU 16000000//crystal
  11.  
  12. /***** Includes *****/
  13. #include <avr/io.h>
  14. //#include <stdint.h>
  15. #include <util/delay.h>
  16. #include <avr/interrupt.h>
  17. //#include <avr/eeprom.h>
  18. //#include <stdio.h>
  19. //#include <string.h>
  20. //#include <avr/pgmspace.h>
  21.  
  22. /***** Hardware macros *****/
  23. //Hardware macros for outputs
  24. #define SET_EW_RED PORTC |= (1<<PC3)
  25. #define CLR_EW_RED PORTC &= ~(1<<PC3)
  26. #define SET_EW_ORN PORTC |= (1<<PC4)
  27. #define CLR_EW_ORN PORTC &= ~(1<<PC4)
  28. #define SET_EW_GRN PORTC |= (1<<PC5)
  29. #define CLR_EW_GRN PORTC &= ~(1<<PC5)
  30. #define SET_NS_RED PORTD |= (1<<PD5)
  31. #define CLR_NS_RED PORTD &= ~(1<<PD5)
  32. #define SET_NS_ORN PORTB |= (1<<PB1)
  33. #define CLR_NS_ORN PORTB &= ~(1<<PB1)
  34. #define SET_NS_GRN PORTB |= (1<<PB0)
  35. #define CLR_NS_GRN PORTB &= ~(1<<PB0)
  36. #define SET_EW_DC PORTC |= (1<<PC0)
  37. #define CLR_EW_DC PORTC &= ~(1<<PC0)
  38. #define SET_EW_CROSS PORTC |= (1<<PC1)
  39. #define CLR_EW_CROSS PORTC &= ~(1<<PC1)
  40. #define SET_NS_DC PORTD |= (1<<PD7)
  41. #define CLR_NS_DC PORTD &= ~(1<<PD7)
  42. #define SET_NS_CROSS PORTD |= (1<<PD6)
  43. #define CLR_NS_CROSS PORTD &= ~(1<<PD6)
  44. //Hardware macros for inputs
  45. #define SW_PED1_IS_LOW ~PINB & (1<<PB5)
  46. #define SW_PED1_IS_HIGH PINB & (1<<PB5)
  47. #define SW_PED2_IS_LOW ~PINB & (1<<PB4)
  48. #define SW_PED2_IS_HIGH PINB & (1<<PB4)
  49. #define SW_PED3_IS_LOW ~PINB & (1<<PB3)
  50. #define SW_PED3_IS_HIGH PINB & (1<<PB3)
  51. #define SW_PED4_IS_LOW ~PINB & (1<<PB2)
  52. #define SW_PED4_IS_HIGH PINB & (1<<PB2)
  53. #define TIMING_TEST_SW_IS_LOW ~PIND & (1<<PD2)
  54. #define TIMING_TEST_SW_IS_HIGH PIND & (1<<PD2)
  55. //Hardware macros for ADC inputs
  56.  
  57. /***** User macros *****/
  58. #define EW_TIME 210
  59. #define NS_TIME 190
  60. #define ALL_RED_TIME 25
  61. #define ORANGE_TIME 16
  62. #define PED_CROSS_TIME 5
  63. #define FLASH_AMOUNT 20
  64. #define FLASH_DELAY 500
  65. #define MS_INCREMENT_AMOUNT 10
  66. #define MS_IN_S 1000
  67.  
  68. /***** Declare & initialise global variables *****/
  69. //DO YOU KNOW WHY THESE ARE VOLATILE??
  70. volatile uint16_t ms_count = 0;
  71. volatile uint16_t secs = 0;
  72. volatile uint8_t ped_activated = 0;
  73.  
  74. /***** Interrupt Service Routines *****/
  75. ISR(INT0_vect){ //THIS IS SETUP SO THAT YOU CAN TEST YOUR CODE
  76. secs = 500;
  77. }
  78. ISR(TIMER0_COMPA_vect) { //called when the timer overflows
  79. ms_count = ms_count+MS_INCREMENT_AMOUNT;
  80. if (ms_count == MS_IN_S){
  81. ms_count = 0;
  82. secs++;
  83. }
  84. }
  85.  
  86. /***** Prototypes for Functions *****/
  87. void poll_ped_sw(); //returns true if any switch is pressed
  88. void ew_go();
  89. void ew_orange();
  90. void ns_go();
  91. void ns_orange();
  92. void all_red();
  93. void peds_cross();
  94. void peds_dont_cross();
  95. void ped_lights_off();
  96. void flash_dont_cross();
  97.  
  98.  
  99. /***** Declare state var and constants *****/
  100. #define ST_EW 1
  101. #define ST_NS 2
  102. #define ST_NS_ORANGE 3
  103. #define ST_EW_ORANGE 4
  104. #define ST_PEDS_DONT_CROSS 5
  105. #define ST_PEDS_CROSS 6
  106. #define ST_ALL_RED 7
  107. uint8_t state = ST_EW;
  108.  
  109. /***** Main function *****/
  110. int main(void) {
  111. /***** Initial hardware setups go here *****/
  112.  
  113. /***** IO Hardware Config *****/
  114. // Initially make all micro pins outputs
  115. DDRB = 0xff; //set as outputs
  116. DDRC = 0xff; //set as outputs
  117. DDRD = 0xff; //set as outputs
  118. // make these pins inputs
  119. DDRB &= ~ (1 << PB5);
  120. DDRB &= ~ (1 << PB4);
  121. DDRB &= ~ (1 << PB3);
  122. DDRB &= ~ (1 << PB2);
  123. DDRD &= ~ (1 << PD2);
  124. PORTB |= (1 << PB5); //activate internal pullup R for SW_PED1
  125. PORTB |= (1 << PB4); //activate internal pullup R for SW_PED2
  126. PORTB |= (1 << PB3); //activate internal pullup R for SW_PED3
  127. PORTB |= (1 << PB2); //activate internal pullup R for SW_PED4
  128. PORTD |= (1 << PD2); //activate internal pullup R for TIMING_TEST_SW
  129.  
  130. /***** Main variables go here *****/
  131. uint8_t last_state = 0;
  132.  
  133. /***** Run once code goes here *****/
  134. EIMSK |= (1 << INT0); // INT0_ENABLE_
  135. EICRA |= (1 << ISC01); //falling edge
  136. // Timer Config setup Timer0 - 2mSec
  137. TCCR0A |= (1 << COM0A1);
  138. TCCR0A |= (1 << COM0A0);
  139. TCCR0B |= (1 << CS02);
  140. TCCR0B |= (1 << CS00);
  141. TCCR0A |= (1 << WGM01);
  142. TIMSK0 |= (1 << OCIE0A);
  143. OCR0A = 155;
  144. sei();
  145.  
  146. /***** Pre-SM Actions *****/
  147. ew_go();
  148.  
  149. /***** Loop code *****/
  150. while (1) {
  151.  
  152. /***** Statemachine *****/
  153.  
  154. /****** ST_EW ******/
  155. while (state == ST_EW) {
  156. poll_ped_sw();
  157. if (secs > EW_TIME) {
  158. state = ST_EW_ORANGE;
  159. ew_orange();
  160.  
  161. secs = 0;
  162. ms_count = 0;
  163. }
  164. }
  165.  
  166. /****** ST_NS ******/
  167. while (state == ST_NS) {
  168. poll_ped_sw();
  169. if (secs > EW_TIME) {
  170. state = ST_NS_ORANGE;
  171. ew_orage();
  172. secs = 0;
  173. ms_count = 0;
  174. }
  175. }
  176.  
  177. /****** ST_NS_ORANGE ******/
  178. while (state == ST_NS_ORANGE) {
  179. poll_ped_sw();
  180. if (ped_activated && secs > ORANGE_TIME) {
  181. state = ST_ALL_RED;
  182. all_red();
  183. secs = 0;
  184. ms_count = 0;
  185. last_state = 0;
  186. }
  187. if (!ped_activated && secs > ORANGE_TIME) {
  188. state = ST_EW;
  189. ns_go();
  190. secs = 0;
  191. ms_count = 0;
  192.  
  193. ;
  194. }
  195. }
  196.  
  197. /****** ST_EW_ORANGE ******/
  198. while (state == ST_EW_ORANGE) {
  199. poll_ped_sw();
  200. if (!ped_activated && secs > ORANGE_TIME) {
  201. state = ST_NS;
  202. ns_go();
  203.  
  204. secs = 0;
  205. ms_count = 0;
  206. }
  207. if (ped_activated && secs > ORANGE_TIME) {
  208. state = ST_ALL_RED;
  209. all_red();
  210. secs = 0;
  211. ms_count = 0;
  212. last_state = 1;
  213. }
  214. }
  215.  
  216. /****** ST_PEDS_DONT_CROSS ******/
  217. while (state == ST_PEDS_DONT_CROSS) {
  218. flash_dont_cross();
  219. if (last_state == 1) {
  220. state = ST_NS;
  221. }
  222. if (last_state == 0) {
  223. state = ST_EW;
  224. }
  225. }
  226.  
  227. /****** ST_PEDS_CROSS ******/
  228. while (state == ST_PEDS_CROSS) {
  229. if (secs > PED_CROSS_TIME) {
  230. state = ST_PEDS_DONT_CROSS;
  231. secs = 0;
  232. ms_count = 0;
  233.  
  234. ;
  235. }
  236. }
  237.  
  238. /****** ST_ALL_RED ******/
  239. while (state == ST_ALL_RED) {
  240. if (secs > 1) {
  241. state = ST_PEDS_CROSS;
  242. peds_cross();
  243. }
  244. }
  245.  
  246. } //while end
  247. } //main end
  248. /***** Functions *****/
  249. void poll_ped_sw() {
  250. if (SW_PED3_IS_LOW | SW_PED2_IS_LOW | SW_PED1_IS_LOW | SW_PED4_IS_LOW) {
  251. ped_activated;
  252. peds_dont_cross();
  253. }
  254.  
  255. }
  256.  
  257. //IT IS USEFUL TO SETUP THE FULL CONDITIONS FOR EACH VEHICLE LIGHT IN EACH FUNCTION
  258. //THIS WAY WE AVOID ANY CONFUSION ABOUT WHAT LIGHTS SHOULD BE TURNED ON AND OFF
  259. void ew_go() {
  260. SET_EW_GRN;
  261. CLR_EW_ORN;
  262. CLR_EW_RED;
  263. CLR_NS_GRN;
  264. CLR_NS_ORN;
  265. SET_NS_RED;
  266. }
  267. void ew_orange() {
  268. CLR_EW_GRN;
  269. SET_EW_ORN;
  270. CLR_EW_RED;
  271. CLR_NS_GRN;
  272. CLR_NS_ORN;
  273. SET_NS_RED;
  274. }
  275. void ns_go() {
  276. CLR_EW_GRN;
  277. CLR_EW_ORN;
  278. SET_EW_RED;
  279. SET_NS_GRN;
  280. CLR_NS_ORN;
  281. CLR_NS_RED;
  282. }
  283. void ns_orange() {
  284. CLR_EW_GRN;
  285. CLR_EW_ORN;
  286. SET_EW_RED;
  287. CLR_NS_GRN;
  288. SET_NS_ORN;
  289. CLR_NS_RED;
  290. }
  291.  
  292. //BEFORE THE PED CROSSES THERE IS A BRIEF PERIOD WHEN
  293. //WE WANT TO MAKE SURE THAT ALL RED LIGHTS ARE ON AND GRN & ORANGE ARE ON
  294. void all_red() {
  295. CLR_EW_GRN;
  296. CLR_EW_ORN;
  297. SET_EW_RED;
  298. SET_EW_DC;
  299. CLR_EW_CROSS;
  300. CLR_NS_GRN;
  301. CLR_NS_ORN;
  302. SET_NS_RED;
  303. SET_NS_DC;
  304. CLR_NS_CROSS;
  305. }
  306. //
  307. void peds_cross() {
  308. CLR_EW_GRN;
  309. CLR_EW_ORN;
  310. SET_EW_RED;
  311. CLR_EW_DC;
  312. SET_EW_CROSS;
  313. CLR_NS_GRN;
  314. CLR_NS_ORN;
  315. SET_NS_RED;
  316. CLR_NS_DC;
  317. SET_NS_CROSS;
  318.  
  319. }
  320. void peds_dont_cross() {
  321. CLR_NS_CROSS;
  322. CLR_EW_CROSS;
  323. SET_NS_DC;
  324. SET_EW_DC;
  325. }
  326. void ped_lights_off() {
  327. CLR_NS_CROSS;
  328. CLR_EW_CROSS;
  329. CLR_NS_DC;
  330. CLR_EW_DC;
  331. }
  332. void flash_dont_cross() {
  333. CLR_NS_CROSS;
  334. CLR_EW_CROSS;
  335. for (int i = 0; i < FLASH_AMOUNT; i++) {
  336. CLR_NS_DC;
  337. CLR_EW_DC;
  338. while (ms_count < 500) {
  339. //blocking
  340. }
  341. SET_NS_DC;
  342. SET_EW_DC;
  343. }
  344. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement