-
Notifications
You must be signed in to change notification settings - Fork 0
/
IRtransmittest.c
173 lines (150 loc) · 4.54 KB
/
IRtransmittest.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
* IRtransmittest.c
*
* Created: 11/13/2012 2:32:32 PM
* Author: Ryan Causey
*/
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/AVRXlib/AVRX_Clocks.h>
#include <avr/AVRXlib/AVRX_Serial.h>
volatile XUSARTst serialStruct;
volatile int is1;
volatile int isOn;
volatile int temp;
ISR(TCC1_OVF_vect)
{
/*
//repeatedly send 1's
if(TCC1_PER == 600 && is1)
{
TCC0_CTRLA = TC_CLKSEL_OFF_gc;
TCC1_PER = 300;
}
else if(is1)
{
TCC0_CTRLA = TC_CLKSEL_DIV64_gc;
TCC1_PER = 600;
}
//repeatedly send 0's
if(!is1 && isOn)
{
TCC0_CTRLA = TC_CLKSEL_OFF_gc;
isOn = 0; //false
}
else if(!is1)
{
TCC0_CTRLA = TC_CLKSEL_DIV64_gc;
isOn = 1; //true
}*/
TCC0_CTRLA = TC_CLKSEL_OFF_gc;
TCC1_CTRLA = TC_CLKSEL_OFF_gc;
}
ISR(USARTE1_TXC_vect)
{
Tx_Handler(&serialStruct);
}
ISR(USARTE1_RXC_vect)
{
Rx_Handler(&serialStruct);
}
ISR(PORTF_INT0_vect)
{
USART_send(&serialStruct, "A falling edge was detected.");
//PORTH_OUT = ++temp;
}
void main(void)
{
unsigned long sClk, pClk;
char recieveString[100];
is1 = 0; //false
isOn = 0; //false
temp = 0;
cli(); //
SetSystemClock(CLK_SCLKSEL_RC32M_gc, CLK_PSADIV_1_gc,
CLK_PSBCDIV_1_1_gc);
GetSystemClocks(&sClk, &pClk);
/*
* Programmable interrupt controller configuration
*/
PMIC_CTRL = PMIC_HILVLEN_bm | PMIC_MEDLVLEN_bm | PMIC_LOLVLEN_bm; //enable all levels of interrupts
/*
* Timer port C0 configuration
* The current values of PER and CCA for this timer will result in the 38Khz oscillating signal needed
* in order to generate 1's and 0's that the reciever recognizes. Do not change these values.
*/
TCC0_CTRLA = TC_CLKSEL_OFF_gc; //set timer to be off intially until serial input happens.
TCC0_CTRLB = 0x10 | TC_WGMODE_SS_gc; //turn on capture(CCAEN) and set waveform generation mode to PWM
TCC0_CTRLC = 0x00; //turn off compares
TCC0_CTRLD = 0x00; //turn off events
TCC0_CTRLE = 0x00; //turn off byte mode
TCC0_PER = 12; //set the top of the period
TCC0_CCA = 6; //set the compare register value to achieve 50% duty cycle at start
/*
* Timer port C1 configuration
* This timer is used to turn the PWM on and off as to replicate 1's and 0's
* The standard requires that a 1 be represented with 1.2ms of 38Khz oscillating signal followed by
* .6ms of no signal. A 0 is represented with .6ms of 38Khz oscillating signal followed by .6ms of
* no signal.
*/
TCC1_CTRLA = TC_CLKSEL_OFF_gc; //set timer to be off intially until serial input happens.
TCC1_CTRLB = TC_WGMODE_NORMAL_gc; //set waveform generation mode to normal
TCC1_CTRLC = 0x00; //turn off compares
TCC1_CTRLD = 0x00; //turn off events
TCC1_CTRLE = 0x00; //turn off byte mode
TCC1_PER = 600; //set the top of the period to overflow at 1.2ms. NOTE: to get .6ms set this to 300.
TCC1_INTCTRLA = 0x01; //set timer c1 overflow interrupts to low level
/*
* Port C configuration
* Set direction to output
*/
PORTC_DIR = 0xFF;
/*
* Port H configuration
* Just turn off LED's
*/
PORTH_DIR = 0xFF; //output dir for all pins
PORTH_OUT = 0x00;
/*
* Serial set up
*/
//initialize the usart e0 for 57600 baud with 8 data bits, no parity, and 1 stop bit, interrupts on low (porth set to this for debugging purposes)
PORTH_OUT = USART_init(&serialStruct, 0xE1, pClk, (_USART_RXCIL_MED | _USART_TXCIL_MED), 576, -4, _USART_CHSZ_8BIT, _USART_PM_DISABLED, _USART_SM_1BIT);
USART_buffer_init(&serialStruct, 100, 100); //initialize the circular buffers
USART_enable(&serialStruct, USART_TXEN_bm | USART_RXEN_bm); //enable the USART
serialStruct.fOutMode = _OUTPUT_CRLF; //append a carriage return and a line feed to every output.
serialStruct.fInMode = _INPUT_CR | _INPUT_TTY | _INPUT_ECHO; //echo input back to the terminal and set up for keyboard input.
/*
* Port F configuration
*/
PORTF_DIR = 0x00; //all pins as input
PORTF_INTCTRL = 0x01; //turn on interrupt 0 with a low priority
PORTF_INT0MASK = 0x04; //mask so that only pin 0 can fire an interrupt;
PORTF_PIN2CTRL = 0x02; //set pin 0 to detect a falling edge as the receiver is active low.*/
sei();
while(1)
{
if(serialStruct.serStatus & _USART_RX_DONE)
{
USART_read(&serialStruct, recieveString);
switch(recieveString[0])
{
case '0':
TCC0_CTRLA = TC_CLKSEL_DIV64_gc;
TCC1_PER = 300;
TCC1_CTRLA = TC_CLKSEL_DIV64_gc;
USART_send(&serialStruct, "Sending a 0");
is1 = 0; //false
break;
case '1':
TCC0_CTRLA = TC_CLKSEL_DIV64_gc;
TCC1_PER = 600;
TCC1_CTRLA = TC_CLKSEL_DIV64_gc;
USART_send(&serialStruct, "Sending a 1");
is1 = 1; //true
isOn = 1; //true
break;
}
}
}
}