View difference between Paste ID: mtZchfyg and 8awzYzYC
SHOW: | | - or go back to the newest paste.
1
/*
2
Copyright 2012 D Westaby
3
 
4
----------------------------------------------------------------------
5
   Car Lights for Attiny2313
6
----------------------------------------------------------------------
7-
Title:      Car_Lighting.c
7+
Title:      Car_Lighting.D
8
Author:     Dustin Westaby
9
Date Created:   September 8, 2012
10-
Date Modified:   September 21, 2012
10+
Date Modified:   September 23, 2012
11
 
12
Compiled with AVR-GCC WinAVR
13
 
14
----------------------------------------------------------------------
15
    Fuses:
16
----------------------------------------------------------------------
17
 BrownOut Disabled
18
 CKDIV8
19
 Int RC Osc 8Mhz + 64ms
20
 
21
----------------------------------------------------------------------
22
    Inputs:
23
----------------------------------------------------------------------
24
 None
25
 
26
----------------------------------------------------------------------
27
    Ouputs:
28
----------------------------------------------------------------------
29
 pin  i/o  port    circuit trace       connection
30
----------------------------------------------------------------------
31
  4    1   PA1 =   R_LED1
32
  5    1   PA0 =   L_LED2
33
  2    1   PD0 =   R_LED3
34
  3    1   PD1 =   R_LED4
35
  6    1   PD2 =   R_LED5
36
  7    1   PD3 =   R_LED6
37
 14    1   PB2 =   L_LED7
38
 17    1   PB5 =   L_LED8
39
 18    1   PB6 =   L_LED9
40
 19    1   PB7 =   L_LED10
41
 
42
*/
43
 
44-
/*--------------------------------------
44+
45-
            Global Variables           |
45+
//          Global Variables           |
46-
---------------------------------------- */
46+
47
// 8 MHz Internal Oscillator DIV8 (used for delay subroutines)
48-
/* 8 MHz Internal Oscillator DIV8 (used for delay subroutines)
48+
// One CPU Cycle = 1us
49-
   One CPU Cycle = 1us */
49+
50
 
51
#define R_LED1_PA1  (1)
52-
#define R_LED1_PA1  (1<<1)
52+
#define L_LED2_PA0  (0)
53-
#define L_LED2_PA0  (1<<0)
53+
#define R_LED3_PD0  (0)
54-
#define R_LED3_PD0  (1<<0)
54+
#define R_LED4_PD1  (1)
55-
#define R_LED4_PD1  (1<<1)
55+
#define R_LED5_PD2  (2)
56-
#define R_LED5_PD2  (1<<2)
56+
#define R_LED6_PD3  (3)
57-
#define R_LED6_PD3  (1<<3)
57+
#define L_LED7_PB2  (2)
58-
#define L_LED7_PB2  (1<<2)
58+
#define L_LED8_PB5  (5)
59-
#define L_LED8_PB5  (1<<5)
59+
#define L_LED9_PB6  (6)
60-
#define L_LED9_PB6  (1<<6)
60+
#define L_LED10_PB7 (7)
61-
#define L_LED10_PB7 (1<<7)
61+
62
63-
  //delay wait values, measured in us
63+
#define CONST_DIM_DELAY_ON  (200)
64-
#define DIM_DELAY_ON  (200)
64+
#define CONST_DIM_DELAY_OFF (4000)
65-
#define DIM_DELAY_OFF (200)
65+
66
/* This value is used to count us of delay and convert to ms
67
   Due to other cpu processing: "1ms of delay" < "1000us of delay" */
68
//#define TIME_CONVERSION_VAL (50) //actual number should be 150 (am running faster)
69-
#define TIME_CONVERSION_VAL (50)
69+
#define TIME_CONVERSION_VAL (150)
70
 
71
int program_ms_counter;
72
int program_counter_us; //used for tracking time elapsed
73
 
74
//--------------------------------------
75
//              Includes               |
76
//--------------------------------------
77
#include <avr/io.h>
78
#include <util/delay.h>
79
 
80
//--------------------------------------
81
//          Delay Subroutines          |
82
//--------------------------------------
83
//These functions are from the delay.h include, the calls to delay functions
84
//are re-written here to allow for longer waits.
85
void delay_ms(uint16_t ms) {
86
        program_ms_counter+=ms;
87
        while ( ms )
88
        {
89
                _delay_ms(1);
90
                ms--;
91
        }
92
}
93
void delay_us(uint16_t us) {
94
  program_counter_us+=us;
95
  while ( us )
96
  {
97
    _delay_us(1);
98
    us--;
99
  }
100
 
101
  while(program_counter_us>=TIME_CONVERSION_VAL)
102-
  //This loop will continue to convert us into ms until the "us counter" is
102+
103-
  //   below the conversion val
103+
          program_ms_counter++;
104
          program_counter_us-=TIME_CONVERSION_VAL;
105
  }
106-
          program_ms_counter++;                      //increment "ms counter"
106+
107-
          program_counter_us-=TIME_CONVERSION_VAL;   //subtract "us counter"
107+
 
108
//--------------------------------------
109
//          Misc Subroutines          |
110
//--------------------------------------
111
 
112-
//         Program Subroutines         |
112+
113
        program_ms_counter=0;
114
        program_counter_us=0;
115
}
116-
   //set "ms counter" and "us counter" to 0
116+
 
117-
   program_ms_counter=0;
117+
void all_LEDs_ON(void) {
118-
   program_counter_us=0;
118+
        PORTA = 0xFF;
119
        PORTB = 0xFF;
120
        PORTD = 0xFF;
121-
void all_LEDs_BRT(void) {
121+
122-
   //Sets all PORT outputs to 1, which is a high (+5V) on the pin
122+
 
123-
   PORTA = 0xFF; // = 0b11111111
123+
124-
   PORTB = 0xFF;
124+
        PORTA = ~(0x03);
125-
   PORTD = 0xFF;
125+
        PORTB = ~(0xFF);
126
        PORTD = ~(0xFF);
127
}
128
 
129-
   //Sets all PORT outputs to 0, which is a low (GND) on the pin
129+
void program_left_turn_mode(void) {
130-
   Porta &= ~(  R_Led1_Pa1   //Turn Off Port A, Pin 1
130+
        PORTA &= ~((1<<R_LED1_PA1)+(1<<L_LED2_PA0));
131-
              + L_Led2_Pa0); //Turn Off Port A, Pin 0
131+
        PORTB &= ~((1<<L_LED10_PB7)+(1<<L_LED8_PB5));
132-
   Portb &= ~(  L_Led10_Pb7  //Turn Off Port B, Pin 7
132+
        PORTD &= ~((1<<R_LED6_PD3)+(1<<R_LED5_PD2)+(1<<R_LED4_PD1)+(1<<R_LED3_PD0));
133-
              + L_Led9_Pb6   //Turn Off Port B, Pin 6
133+
134-
              + L_Led8_Pb5   //Turn Off Port B, Pin 5
134+
135-
              + L_Led7_Pb2); //Turn Off Port B, Pin 2
135+
void program_brake_mode(void) {
136-
   Portd &= ~(  R_Led6_Pd3   //Turn Off Port D, Pin 3
136+
        PORTA &= ~((1<<R_LED1_PA1)+(1<<L_LED2_PA0));
137-
              + R_Led5_Pd2   //Turn Off Port D, Pin 2
137+
138-
              + R_Led4_Pd1   //Turn Off Port D, Pin 1
138+
139-
              + R_Led3_Pd0); //Turn Off Port D, Pin 0
139+
void program_right_turn_mode (void) {
140
        PORTA &= ~((1<<R_LED1_PA1)+(1<<L_LED2_PA0));
141
        PORTB &= ~((1<<L_LED10_PB7)+(1<<L_LED9_PB6)+(1<<L_LED8_PB5)+(1<<L_LED7_PB2));
142-
void program2_LEDs_to_DIM(void) {
142+
        PORTD &= ~((1<<R_LED6_PD3)+(1<<R_LED4_PD1));
143-
   PORTA &= ~(  R_LED1_PA1   //Turn Off PORT A, pin 1
143+
144-
              + L_LED2_PA0); //Turn Off PORT A, pin 0
144+
145-
   PORTB &= ~(  L_LED10_PB7  //Turn Off PORT B, pin 7
145+
void program_all_dim_loop (int timer_val) {
146-
              + L_LED8_PB5); //Turn Off PORT B, pin 5
146+
	
147-
   PORTD &= ~(  R_LED6_PD3   //Turn Off PORT D, pin 3
147+
	while(program_ms_counter < timer_val)
148-
              + R_LED5_PD2   //Turn Off PORT D, pin 2
148+
	{
149-
              + R_LED4_PD1   //Turn Off PORT D, pin 1
149+
		all_LEDs_ON();
150-
              + R_LED3_PD0); //Turn Off PORT D, pin 0
150+
		delay_us(CONST_DIM_DELAY_ON);
151
		all_LEDs_OFF();
152
		delay_us(CONST_DIM_DELAY_OFF);
153-
void program3_6_LEDs_to_DIM(void) {
153+
	}
154-
   PORTA &= ~(  R_LED1_PA1   //Turn Off PORT A, pin 1
154+
	
155-
              + L_LED2_PA0); //Turn Off PORT A, pin 0
155+
156
157
void program_break_loop (int timer_val) {
158-
void program5_LEDs_to_DIM (void) {
158+
	
159-
   PORTA &= ~(  R_LED1_PA1   //Turn Off PORT A, pin 1
159+
	while(program_ms_counter < timer_val)
160-
              + L_LED2_PA0); //Turn Off PORT A, pin 0
160+
	{
161-
   PORTB &= ~(  L_LED10_PB7  //Turn Off PORT B, pin 7
161+
		all_LEDs_ON();
162-
              + L_LED9_PB6   //Turn Off PORT B, pin 6
162+
		delay_us(CONST_DIM_DELAY_ON);
163-
              + L_LED8_PB5   //Turn Off PORT B, pin 5
163+
		program_brake_mode();
164-
              + L_LED7_PB2); //Turn Off PORT B, pin 2
164+
		delay_us(CONST_DIM_DELAY_OFF);
165-
   PORTD &= ~(  R_LED6_PD3   //Turn Off PORT D, pin 3
165+
	}
166-
              + R_LED4_PD1); //Turn Off PORT D, pin 1
166+
	
167
}
168
169-
/* Software PWM explaination:
169+
void program_left_turn_loop (int timer_val) {
170
	
171-
   The dim LED effect is performed by a software PWM.  Each of the loops in the
171+
	while(program_ms_counter < timer_val)
172-
   main() function turn on all LEDs, then turn off only the LEDs we want as DIM.
172+
	{
173-
   The LEDs that remain on will appear BRIGHT.  The LEDs that are rapid turned
173+
		all_LEDs_ON();
174-
   on and off will appear DIM.
174+
		delay_us(CONST_DIM_DELAY_ON);
175
		program_left_turn_mode();
176-
                      DIM_DELAY_ON                 DIM_DELAY_ON
176+
		delay_us(CONST_DIM_DELAY_OFF);
177-
                +5V  ______________               _______________
177+
	}
178-
                     |            |               |             |
178+
	
179-
 GND  _______________|            |_______________|             |______________
179+
180-
       DIM_DELAY_OFF                DIM_DELAY_OFF                DIM_DELAY_OFF
180+
181
void program_right_turn_loop (int timer_val) {
182
	
183-
void program_NORMAL_MODE(void) {
183+
	while(program_ms_counter < timer_val)
184-
   all_LEDs_BRT();           //Turn on all LEDs
184+
	{
185-
   delay_us(DIM_DELAY_ON);   //Time to keep all LEDs on
185+
		all_LEDs_ON();
186-
   all_LEDs_OFF();           //Turn off all LEDs
186+
		delay_us(CONST_DIM_DELAY_ON);
187-
   delay_us(DIM_DELAY_OFF);  //Time to keep all LEDs off
187+
		program_right_turn_mode();
188
		delay_us(CONST_DIM_DELAY_OFF);
189
	}
190-
void program_LEFT_TURN(void) {
190+
	
191-
   all_LEDs_BRT();           //Turn on all LEDs
191+
192-
   delay_us(DIM_DELAY_ON);   //Time to keep all LEDs on
192+
193-
   program2_LEDs_to_DIM();   //Turn off program 2 LEDs
193+
194-
   delay_us(DIM_DELAY_OFF);  //Time to keep program 2 LEDs off
194+
195
//               Main                  |
196
//--------------------------------------
197-
void program_BRAKE_MODE(void) {
197+
198-
   all_LEDs_BRT();           //Turn on all LEDs
198+
199-
   delay_us(DIM_DELAY_ON);   //Time to keep all LEDs on
199+
 
200-
   program3_6_LEDs_to_DIM();   //Turn off program 3 LEDs
200+
201-
   delay_us(DIM_DELAY_OFF);  //Time to keep program 3 LEDs off
201+
202
  /* ---------------------------------------------------------------- */
203
  
204-
void program_RIGHT_TURN (void) {
204+
205-
   all_LEDs_BRT();           //Turn on all LEDs
205+
206-
   delay_us(DIM_DELAY_ON);   //Time to keep all LEDs on
206+
207-
   program5_LEDs_to_DIM();   //Turn off program 5 LEDs
207+
208-
   delay_us(DIM_DELAY_OFF);  //Time to keep program 5 LEDs off
208+
 
209
  all_LEDs_OFF();
210
 
211
/* ---------------------------------------------------------------- */
212
/*                              Main Loop                           */
213
/* ---------------------------------------------------------------- */
214
 
215
  while(1)
216
  {
217
 
218
		//1 - All dim nothing happens (resting natural state)
219
		reset_ms_us_counters();
220
		program_all_dim_loop(6000);
221
        
222
		//2 - left turn, all dim, 2 will blink at ½ sec for 5 sec. 
223
		reset_ms_us_counters();
224
		while(program_ms_counter < 5000) 
225
		{
226
			program_left_turn_loop(program_ms_counter+500);
227-
  //Begin with all LEDs off
227+
			program_all_dim_loop  (program_ms_counter+500);
228
		}
229
		//then dim for 1 sec
230
		program_all_dim_loop(6000);     
231
232
		//3 – brakes get bright (8 LED’s) for 2 sec then dim for 1 for 6 seconds
233
		reset_ms_us_counters();
234
		while(program_ms_counter < 6000)
235
		{
236
			program_break_loop  (program_ms_counter+2000);
237-
        /* ---------------------------- */
237+
			program_all_dim_loop(program_ms_counter+1000);
238-
        /*      Program 1 - NORMAL      */
238+
		}
239-
        /* ---------------------------- */
239+
240-
        reset_ms_us_counters();
240+
		//4 - All dim nothing happens (resting natural state)
241
		reset_ms_us_counters();
242-
        while(program_ms_counter < 6000)
242+
		program_all_dim_loop(6000);
243
244-
           program_NORMAL_MODE();
244+
		//5 – right turn, all dim, 2 will blink at ½ sec for 5 sec.  
245
		reset_ms_us_counters();
246
		while(program_ms_counter < 5000)
247-
        /* ---------------------------- */
247+
		{
248-
        /*      Program 3 - BRAKES      */
248+
			program_right_turn_loop(program_ms_counter+500);
249-
        /* ---------------------------- */
249+
			program_all_dim_loop   (program_ms_counter+500);
250-
        reset_ms_us_counters();
250+
		}
251
		//then dim for 1 sec.
252-
        while(program_ms_counter < 500)
252+
		program_all_dim_loop(6000);
253
254-
           program_BRAKE_MODE();
254+
		//6 – brakes get bright (8 LED’s) for 2 sec then dim for 1 for 6 seconds
255
		reset_ms_us_counters();
256-
        while(program_ms_counter < 1000)
256+
		while(program_ms_counter < 6000)
257
		{
258-
           program_NORMAL_MODE();
258+
			program_break_loop  (program_ms_counter+2000);
259
			program_all_dim_loop(program_ms_counter+1000);
260-
        while(program_ms_counter < 1500)
260+
		}
261
262-
           program_BRAKE_MODE();
262+
263
 
264-
        while(program_ms_counter < 2000)
264+
265
 
266-
           program_NORMAL_MODE();
266+