SlideShare une entreprise Scribd logo
1  sur  17
Télécharger pour lire hors ligne
Some Exercises with Timers and UART
Corrado Santoro
ARSLAB - Autonomous and Robotic Systems Laboratory
Dipartimento di Matematica e Informatica - Universit`a di Catania, Italy
santoro@dmi.unict.it
L.A.P. 1 Course
Corrado Santoro Some Exercises with Timers and UART
Exercise 1: A light with timer
We want to manage an ambient light with a timer with the
following specifications:
A pushbutton (PB1) turn on the light
Another pushbutton (PB2) turn off the light
The light remains on for 10 seconds, then it turns off
automatically
If, during the 10 seconds, PB1 is pushed again, the timer
restarts
After 5 seconds, a LED starts to flash with a period of
500 ms, indicating that the light is going to be turned off
Corrado Santoro Some Exercises with Timers and UART
A light with timer: connections
PB1: connected to RA3
PB2: connected to RA2
Light: simulated with LED connected to RB0
Signalling LED connected to RB5
§
#define PB1 PORTAbits.RA3
#define PB2 PORTAbits.RA2
#define LIGHT LATBbits.LATB0
#define SIGNAL LATBbits.LATB5
¦ ¥
Corrado Santoro Some Exercises with Timers and UART
A light with timer: tasks
Task 1, manage timer, light and signalling
Task 2, manage pushbuttons
Task 1 is timer-dependent, we can manage it using interrupts
by setting the overflow to 500 ms, which is the least timer value
in our problem.
Corrado Santoro Some Exercises with Timers and UART
Timer Setup
We want to use the system clock, T0CS = 0;;
We have FOSC = 64MHz, therefore the basic frequency is
FOSC/4 = 16MHz, the P = 62.5ns;
Let’s use the prescaler and divide the frequency by 256, so PSA = 0;
T0PS = 0b111;
The timer increments using a period P = 62.5ns ∗ 256 = 16µs.
So 500ms/16µs = 31250 counts.
Corrado Santoro Some Exercises with Timers and UART
Timer Setup
§
#define TIMER_VAL -31250
void timer_setup(void)
{
T0CONbits.TMR0ON = 0; // stop the timer
T0CONbits.T08BIT = 0; // timer configured as 16-bit
T0CONbits.T0CS = 0; // use FOSC
T0CONbits.PSA = 0; // use prescaler
T0CONbits.T0PS = 0b111; // prescaler 1:256
TMR0 = TIMER_VAL;
T0CONbits.TMR0ON = 1; // start the timer
INTCONbits.T0IF = 0; // reset timer interrupt flag
INTCONbits.T0IE = 1; // enable timer interrupts
RCONbits.IPEN = 0; // no priorities
INTCONbits.PEIE = 1; // enable peripheral interrupts
INTCONbits.GIE = 1; // enable interrupts globally
}
¦ ¥
Corrado Santoro Some Exercises with Timers and UART
Timer Task
Two variables:
light status, boolean, indicates the status of the light
tick count, integer (8 bit), incremented in the timer ISR in
order to count “timeouts” of 500 ms
§
unsigned char light_status, tick_count; // 8 bit vars
#define ON 0
#define OFF 1
void interrupt isr(void)
{
if (INTCONbits.T0IF == 1) {
TMR0 = TIMER_VAL;
if (light_status == 0) {
LIGHT = OFF;
SIGNAL = OFF;
tick_count = 0;
}
else {
// ... see next slide
}
INTCONbits.T0IF = 0;
}
}
¦ ¥
Corrado Santoro Some Exercises with Timers and UART
Timer Task (2)
§
...
else { // light_status == 1
LIGHT = ON;
tick_count++;
if (tick_count = 20) { // 20*500 = 10 sec
light_status = 0;
LIGHT = OFF;
SIGNAL = OFF;
}
else if (tick_count = 10) { // 10*500 = 5 sec
SIGNAL = !SIGNAL; // flashing
}
}
...
¦ ¥
Corrado Santoro Some Exercises with Timers and UART
Task 2: “User interface” and Main Program
§
void main(void)
{
TRISAbits.TRISA3 = 1; // input
TRISAbits.TRISA2 = 1; // input
TRISBbits.TRISB0 = 0; // output
TRISBbits.TRISB5 = 0; // output
light_status = 0;
tick_count = 0;
timer_setup();
for (;;) {
if (PB1 == ON) { // turn on the light and re-arm timer
light_status = 1;
tick_count = 0;
}
if (PB2 == ON) { // turn off the light
light_status = 0;
}
}
}
¦ ¥
Corrado Santoro Some Exercises with Timers and UART
Exercise 2: A light with configurable timer
We want to add to the previous program the ability to configure
timeouts using some commands sent through the UART:
The sequence “***”+CR enters configuration mode
Two commands:
timeout VAL, sets the “light off” timeout
signal VAL, sets the timer value for the flashing signal
end, exits configuration mode
The program is a little bit more complex!
Corrado Santoro Some Exercises with Timers and UART
First let’s add timer variables
§
unsigned char off_timer = 20, signal_timer = 10;
...
else { // light_status == 1
LIGHT = ON;
tick_count++;
if (tick_count = off_timer) {
light_status = 0;
LIGHT = OFF;
SIGNAL = OFF;
}
else if (tick_count = signal_timer) {
SIGNAL = !SIGNAL; // flashing
}
}
...
¦ ¥
Corrado Santoro Some Exercises with Timers and UART
UART Setup
§
void uart_setup(void)
{
TRISCbits.TRISC6 = 0; // TX as output
TRISCbits.TRISC7 = 1; // RX as input
TXSTA1bits.SYNC = 0; // Async operation
TXSTA1bits.TX9 = 0; // No tx of 9th bit
TXSTA1bits.TXEN = 1; // Enable transmitter
RCSTA1bits.RX9 = 0; // No rx of 9th bit
RCSTA1bits.CREN = 1; // Enable receiver
RCSTA1bits.SPEN = 1; // Enable serial port
// Setting for 19200 BPS
BAUDCON1bits.BRG16 = 0; // Divisor at 8 bit
TXSTA1bits.BRGH = 0; // No high-speed baudrate
SPBRG1 = 51; // divisor value for 19200
}
¦ ¥
Corrado Santoro Some Exercises with Timers and UART
UART character reading, blocking and non-blocking
§
char read_char(void)
{
while (PIR1bits.RC1IF == 0) { // wait for char
if (RCSTA1bits.OERR == 1) {
RCSTA1bits.OERR = 0; // clear overrun if it occurs
RCSTA1bits.CREN = 0;
RCSTA1bits.CREN = 1;
}
}
return RCREG1;
}
int nb_read_char(char * c_ptr) // non blocking version
{
if (RCSTA1bits.OERR == 1) { // check for errors in anycase
RCSTA1bits.OERR = 0; // clear overrun if it occurs
RCSTA1bits.CREN = 0;
RCSTA1bits.CREN = 1;
}
if (PIR1bits.RC1IF == 0) {
return 0; // no char available
}
else { // get char
*c_ptr = RCREG1;
return 1;
}
}
¦ ¥
Corrado Santoro Some Exercises with Timers and UART
UART string reading and character output
§
void putch(char c) {
// wait the end of transmission
while (TXSTA1bits.TRMT == 0) {};
TXREG1 = c; // send the new byte
}
void read_line(char * s)
{
for (;;) {
char c = read_char();
if (c  ’ ’) { // it is a control char
if (c == 0x0d) { // CR
putch(13);
*s = 0;
return;
}
}
else { // it is a printable char
putch(c);
*s = c;
++s;
}
}
}
¦ ¥
Corrado Santoro Some Exercises with Timers and UART
Exercise 2: Main Program
§
void main(void) {
int asterisk_count = 0;
TRISAbits.TRISA3 = 1; // input
TRISAbits.TRISA2 = 1; // input
TRISBbits.TRISB0 = 0; // output
TRISBbits.TRISB5 = 0; // output
light_status = 0;
tick_count = 0;
timer_setup();
uart_setup();
for (;;) {
char c;
if (PB1 == ON) { // turn on the light and re-arm timer
light_status = 1;
tick_count = 0;
}
if (PB2 == ON) // turn off the light
light_status = 0;
if (nb_read_char(c) == 1) { // a char is got
if (c == ’*’) {
++asterisk_count;
if (asterisk_count == 3) {
enter_config();
asterisk_count = 0;
}
}
else
asterisk_count = 0;
}
}
}
¦ ¥
Corrado Santoro Some Exercises with Timers and UART
Exercise 2: Configuration Function
§
void enter_config(void)
{
char command[40];
printf(CONFIG);
for (;;) {
read_line(command);
if (strcmp(command,end) == 0) {
printf(END OF CONFIGURATIONn);
return;
}
else if (strncmp(command,timeout,7) == 0)
off_timer = atoi(command+7) * 2;
else if (strncmp(command,signal,6) == 0)
signal_timer = atoi(command+6) * 2;
else
printf(Invalid commandn);
}
}
¦ ¥
Corrado Santoro Some Exercises with Timers and UART
Some Exercises with Timers and UART
Corrado Santoro
ARSLAB - Autonomous and Robotic Systems Laboratory
Dipartimento di Matematica e Informatica - Universit`a di Catania, Italy
santoro@dmi.unict.it
L.A.P. 1 Course
Corrado Santoro Some Exercises with Timers and UART

Contenu connexe

Tendances

PIC timer programming
PIC timer programmingPIC timer programming
PIC timer programmingAkash Puri
 
Timer counter in arm7(lpc2148)
Timer counter in arm7(lpc2148)Timer counter in arm7(lpc2148)
Timer counter in arm7(lpc2148)Aarav Soni
 
PIC18 TIMER PROGRAMMING IN ASSEMBLY AND C
PIC18 TIMER PROGRAMMING IN ASSEMBLY AND CPIC18 TIMER PROGRAMMING IN ASSEMBLY AND C
PIC18 TIMER PROGRAMMING IN ASSEMBLY AND C raosandy11
 
AVR_Course_Day7 timers counters and interrupt programming
AVR_Course_Day7 timers counters and  interrupt programmingAVR_Course_Day7 timers counters and  interrupt programming
AVR_Course_Day7 timers counters and interrupt programmingMohamed Ali
 
Timer & Interrupt Atmega16
Timer & Interrupt Atmega16Timer & Interrupt Atmega16
Timer & Interrupt Atmega16Ramadan Ramadan
 
Pt 51 kit - Peripheral self-test
Pt 51 kit - Peripheral self-testPt 51 kit - Peripheral self-test
Pt 51 kit - Peripheral self-testrajbabureliance
 
8051 Timers / Counters
8051 Timers / Counters8051 Timers / Counters
8051 Timers / CountersPatricio Lima
 
Microcontroller Instruction Set atmel
Microcontroller Instruction Set atmelMicrocontroller Instruction Set atmel
Microcontroller Instruction Set atmelRuderocker Billy
 
8051 Timers and Counters
8051 Timers and Counters8051 Timers and Counters
8051 Timers and CountersShreyans Pathak
 
AVR_Course_Day6 external hardware interrupts and analogue to digital converter
AVR_Course_Day6 external hardware  interrupts and analogue to digital converterAVR_Course_Day6 external hardware  interrupts and analogue to digital converter
AVR_Course_Day6 external hardware interrupts and analogue to digital converterMohamed Ali
 

Tendances (20)

Lecture 2 timers, pwm, state machine IN PIC
Lecture 2   timers, pwm, state machine IN PIC Lecture 2   timers, pwm, state machine IN PIC
Lecture 2 timers, pwm, state machine IN PIC
 
Class9
Class9Class9
Class9
 
PIC timer programming
PIC timer programmingPIC timer programming
PIC timer programming
 
Timer counter in arm7(lpc2148)
Timer counter in arm7(lpc2148)Timer counter in arm7(lpc2148)
Timer counter in arm7(lpc2148)
 
PIC18 TIMER PROGRAMMING IN ASSEMBLY AND C
PIC18 TIMER PROGRAMMING IN ASSEMBLY AND CPIC18 TIMER PROGRAMMING IN ASSEMBLY AND C
PIC18 TIMER PROGRAMMING IN ASSEMBLY AND C
 
9 timer programming
9 timer programming9 timer programming
9 timer programming
 
AVR_Course_Day7 timers counters and interrupt programming
AVR_Course_Day7 timers counters and  interrupt programmingAVR_Course_Day7 timers counters and  interrupt programming
AVR_Course_Day7 timers counters and interrupt programming
 
Timer & Interrupt Atmega16
Timer & Interrupt Atmega16Timer & Interrupt Atmega16
Timer & Interrupt Atmega16
 
Avr timers
Avr timersAvr timers
Avr timers
 
Lecture7
Lecture7Lecture7
Lecture7
 
Microcontroller part 3
Microcontroller part 3Microcontroller part 3
Microcontroller part 3
 
8051 ch9-950217
8051 ch9-9502178051 ch9-950217
8051 ch9-950217
 
Pt 51 kit - Peripheral self-test
Pt 51 kit - Peripheral self-testPt 51 kit - Peripheral self-test
Pt 51 kit - Peripheral self-test
 
8051 Timers / Counters
8051 Timers / Counters8051 Timers / Counters
8051 Timers / Counters
 
Interrupt
InterruptInterrupt
Interrupt
 
89c5131datasheet
89c5131datasheet89c5131datasheet
89c5131datasheet
 
Microcontroller Instruction Set atmel
Microcontroller Instruction Set atmelMicrocontroller Instruction Set atmel
Microcontroller Instruction Set atmel
 
8051 Timers and Counters
8051 Timers and Counters8051 Timers and Counters
8051 Timers and Counters
 
AVR_Course_Day6 external hardware interrupts and analogue to digital converter
AVR_Course_Day6 external hardware  interrupts and analogue to digital converterAVR_Course_Day6 external hardware  interrupts and analogue to digital converter
AVR_Course_Day6 external hardware interrupts and analogue to digital converter
 
W10: Interrupts
W10: InterruptsW10: Interrupts
W10: Interrupts
 

Similaire à Exercises with timers and UART

L15 timers-counters-in-atmega328 p
L15 timers-counters-in-atmega328 pL15 timers-counters-in-atmega328 p
L15 timers-counters-in-atmega328 prsamurti
 
SE PAI Unit 5_Timer Programming in 8051 microcontroller_Part 2
SE PAI Unit 5_Timer Programming in 8051 microcontroller_Part 2SE PAI Unit 5_Timer Programming in 8051 microcontroller_Part 2
SE PAI Unit 5_Timer Programming in 8051 microcontroller_Part 2KanchanPatil34
 
Freescale Microcontroller programming
Freescale Microcontroller programmingFreescale Microcontroller programming
Freescale Microcontroller programmingShih Cheng Tung
 
timer counter (1).pptx
timer counter (1).pptxtimer counter (1).pptx
timer counter (1).pptxSujalKumar73
 
[嵌入式系統] MCS-51 實驗 - 使用 IAR (2)
[嵌入式系統] MCS-51 實驗 - 使用 IAR (2)[嵌入式系統] MCS-51 實驗 - 使用 IAR (2)
[嵌入式系統] MCS-51 實驗 - 使用 IAR (2)Simen Li
 
PWM based motor speed control using LPC 1768
PWM based motor speed control using LPC 1768PWM based motor speed control using LPC 1768
PWM based motor speed control using LPC 1768Omkar Rane
 
Assembly programming II
Assembly programming IIAssembly programming II
Assembly programming IIOmar Sanchez
 
Assembly programming II
Assembly programming IIAssembly programming II
Assembly programming IIOmar Sanchez
 
Assembly programming II
Assembly programming IIAssembly programming II
Assembly programming IIOmar Sanchez
 
Programmable Logic Controller | Ladder Logic diagrams| Block diagram | I/O Mo...
Programmable Logic Controller | Ladder Logic diagrams| Block diagram | I/O Mo...Programmable Logic Controller | Ladder Logic diagrams| Block diagram | I/O Mo...
Programmable Logic Controller | Ladder Logic diagrams| Block diagram | I/O Mo...Waqas Afzal
 
8051 Timers and Counters
8051 Timers and Counters8051 Timers and Counters
8051 Timers and Counterscjbas
 
伺服馬達控制
伺服馬達控制伺服馬達控制
伺服馬達控制艾鍗科技
 
Embedded JavaScript
Embedded JavaScriptEmbedded JavaScript
Embedded JavaScriptJens Siebert
 
Timers done by Priyanga KR
Timers done by Priyanga KRTimers done by Priyanga KR
Timers done by Priyanga KRPriyangaKR1
 
Analog to digital converter
Analog to digital converterAnalog to digital converter
Analog to digital converterCorrado Santoro
 

Similaire à Exercises with timers and UART (20)

L15 timers-counters-in-atmega328 p
L15 timers-counters-in-atmega328 pL15 timers-counters-in-atmega328 p
L15 timers-counters-in-atmega328 p
 
SE PAI Unit 5_Timer Programming in 8051 microcontroller_Part 2
SE PAI Unit 5_Timer Programming in 8051 microcontroller_Part 2SE PAI Unit 5_Timer Programming in 8051 microcontroller_Part 2
SE PAI Unit 5_Timer Programming in 8051 microcontroller_Part 2
 
Freescale Microcontroller programming
Freescale Microcontroller programmingFreescale Microcontroller programming
Freescale Microcontroller programming
 
timer counter (1).pptx
timer counter (1).pptxtimer counter (1).pptx
timer counter (1).pptx
 
12 mt06ped007
12 mt06ped007 12 mt06ped007
12 mt06ped007
 
[嵌入式系統] MCS-51 實驗 - 使用 IAR (2)
[嵌入式系統] MCS-51 實驗 - 使用 IAR (2)[嵌入式系統] MCS-51 實驗 - 使用 IAR (2)
[嵌入式系統] MCS-51 實驗 - 使用 IAR (2)
 
PWM based motor speed control using LPC 1768
PWM based motor speed control using LPC 1768PWM based motor speed control using LPC 1768
PWM based motor speed control using LPC 1768
 
Assembly programming II
Assembly programming IIAssembly programming II
Assembly programming II
 
Assembly programming II
Assembly programming IIAssembly programming II
Assembly programming II
 
Assembly programming II
Assembly programming IIAssembly programming II
Assembly programming II
 
8051 ch9
8051 ch98051 ch9
8051 ch9
 
Programmable Logic Controller | Ladder Logic diagrams| Block diagram | I/O Mo...
Programmable Logic Controller | Ladder Logic diagrams| Block diagram | I/O Mo...Programmable Logic Controller | Ladder Logic diagrams| Block diagram | I/O Mo...
Programmable Logic Controller | Ladder Logic diagrams| Block diagram | I/O Mo...
 
8051 Timers and Counters
8051 Timers and Counters8051 Timers and Counters
8051 Timers and Counters
 
伺服馬達控制
伺服馬達控制伺服馬達控制
伺服馬達控制
 
Embedded JavaScript
Embedded JavaScriptEmbedded JavaScript
Embedded JavaScript
 
chapter 4
chapter 4chapter 4
chapter 4
 
AVRTIMER.pptx
AVRTIMER.pptxAVRTIMER.pptx
AVRTIMER.pptx
 
8051 Timer
8051 Timer8051 Timer
8051 Timer
 
Timers done by Priyanga KR
Timers done by Priyanga KRTimers done by Priyanga KR
Timers done by Priyanga KR
 
Analog to digital converter
Analog to digital converterAnalog to digital converter
Analog to digital converter
 

Plus de Corrado Santoro

Physical Flying Agents: Ummanned Aerial Vehicles Control, Coordination and Em...
Physical Flying Agents: Ummanned Aerial Vehicles Control, Coordination and Em...Physical Flying Agents: Ummanned Aerial Vehicles Control, Coordination and Em...
Physical Flying Agents: Ummanned Aerial Vehicles Control, Coordination and Em...Corrado Santoro
 
Pulse Width Modulation Signal Generation with MCUs
Pulse Width Modulation Signal Generation with MCUsPulse Width Modulation Signal Generation with MCUs
Pulse Width Modulation Signal Generation with MCUsCorrado Santoro
 
Presentation @ Miniscuola WOA 2015
Presentation @ Miniscuola WOA 2015Presentation @ Miniscuola WOA 2015
Presentation @ Miniscuola WOA 2015Corrado Santoro
 
Presentation @ WOA 2015
Presentation @ WOA 2015 Presentation @ WOA 2015
Presentation @ WOA 2015 Corrado Santoro
 
Introduction to shell scripting
Introduction to shell scriptingIntroduction to shell scripting
Introduction to shell scriptingCorrado Santoro
 
Programming the Digital I/O Interface of a PIC microcontroller
Programming the Digital I/O Interface of a PIC microcontrollerProgramming the Digital I/O Interface of a PIC microcontroller
Programming the Digital I/O Interface of a PIC microcontrollerCorrado Santoro
 
Introduction to microcontrollers
Introduction to microcontrollersIntroduction to microcontrollers
Introduction to microcontrollersCorrado Santoro
 
How does a Quadrotor fly? A journey from physics, mathematics, control system...
How does a Quadrotor fly? A journey from physics, mathematics, control system...How does a Quadrotor fly? A journey from physics, mathematics, control system...
How does a Quadrotor fly? A journey from physics, mathematics, control system...Corrado Santoro
 
Integrating Cloud Services in Behaviour Programming for Autonomous Robots
Integrating Cloud Services in Behaviour  Programming for Autonomous RobotsIntegrating Cloud Services in Behaviour  Programming for Autonomous Robots
Integrating Cloud Services in Behaviour Programming for Autonomous RobotsCorrado Santoro
 
Reactive Autonomous System Programming using the PROFETA tool
Reactive Autonomous System Programming using the PROFETA toolReactive Autonomous System Programming using the PROFETA tool
Reactive Autonomous System Programming using the PROFETA toolCorrado Santoro
 

Plus de Corrado Santoro (14)

Physical Flying Agents: Ummanned Aerial Vehicles Control, Coordination and Em...
Physical Flying Agents: Ummanned Aerial Vehicles Control, Coordination and Em...Physical Flying Agents: Ummanned Aerial Vehicles Control, Coordination and Em...
Physical Flying Agents: Ummanned Aerial Vehicles Control, Coordination and Em...
 
The I2C Interface
The I2C InterfaceThe I2C Interface
The I2C Interface
 
Pulse Width Modulation Signal Generation with MCUs
Pulse Width Modulation Signal Generation with MCUsPulse Width Modulation Signal Generation with MCUs
Pulse Width Modulation Signal Generation with MCUs
 
Presentation @ Miniscuola WOA 2015
Presentation @ Miniscuola WOA 2015Presentation @ Miniscuola WOA 2015
Presentation @ Miniscuola WOA 2015
 
Presentation @ WOA 2015
Presentation @ WOA 2015 Presentation @ WOA 2015
Presentation @ WOA 2015
 
Soc
SocSoc
Soc
 
Introduction to Erlang
Introduction to ErlangIntroduction to Erlang
Introduction to Erlang
 
Introduction to shell scripting
Introduction to shell scriptingIntroduction to shell scripting
Introduction to shell scripting
 
Pillole di C++
Pillole di C++Pillole di C++
Pillole di C++
 
Programming the Digital I/O Interface of a PIC microcontroller
Programming the Digital I/O Interface of a PIC microcontrollerProgramming the Digital I/O Interface of a PIC microcontroller
Programming the Digital I/O Interface of a PIC microcontroller
 
Introduction to microcontrollers
Introduction to microcontrollersIntroduction to microcontrollers
Introduction to microcontrollers
 
How does a Quadrotor fly? A journey from physics, mathematics, control system...
How does a Quadrotor fly? A journey from physics, mathematics, control system...How does a Quadrotor fly? A journey from physics, mathematics, control system...
How does a Quadrotor fly? A journey from physics, mathematics, control system...
 
Integrating Cloud Services in Behaviour Programming for Autonomous Robots
Integrating Cloud Services in Behaviour  Programming for Autonomous RobotsIntegrating Cloud Services in Behaviour  Programming for Autonomous Robots
Integrating Cloud Services in Behaviour Programming for Autonomous Robots
 
Reactive Autonomous System Programming using the PROFETA tool
Reactive Autonomous System Programming using the PROFETA toolReactive Autonomous System Programming using the PROFETA tool
Reactive Autonomous System Programming using the PROFETA tool
 

Exercises with timers and UART

  • 1. Some Exercises with Timers and UART Corrado Santoro ARSLAB - Autonomous and Robotic Systems Laboratory Dipartimento di Matematica e Informatica - Universit`a di Catania, Italy santoro@dmi.unict.it L.A.P. 1 Course Corrado Santoro Some Exercises with Timers and UART
  • 2. Exercise 1: A light with timer We want to manage an ambient light with a timer with the following specifications: A pushbutton (PB1) turn on the light Another pushbutton (PB2) turn off the light The light remains on for 10 seconds, then it turns off automatically If, during the 10 seconds, PB1 is pushed again, the timer restarts After 5 seconds, a LED starts to flash with a period of 500 ms, indicating that the light is going to be turned off Corrado Santoro Some Exercises with Timers and UART
  • 3. A light with timer: connections PB1: connected to RA3 PB2: connected to RA2 Light: simulated with LED connected to RB0 Signalling LED connected to RB5 § #define PB1 PORTAbits.RA3 #define PB2 PORTAbits.RA2 #define LIGHT LATBbits.LATB0 #define SIGNAL LATBbits.LATB5 ¦ ¥ Corrado Santoro Some Exercises with Timers and UART
  • 4. A light with timer: tasks Task 1, manage timer, light and signalling Task 2, manage pushbuttons Task 1 is timer-dependent, we can manage it using interrupts by setting the overflow to 500 ms, which is the least timer value in our problem. Corrado Santoro Some Exercises with Timers and UART
  • 5. Timer Setup We want to use the system clock, T0CS = 0;; We have FOSC = 64MHz, therefore the basic frequency is FOSC/4 = 16MHz, the P = 62.5ns; Let’s use the prescaler and divide the frequency by 256, so PSA = 0; T0PS = 0b111; The timer increments using a period P = 62.5ns ∗ 256 = 16µs. So 500ms/16µs = 31250 counts. Corrado Santoro Some Exercises with Timers and UART
  • 6. Timer Setup § #define TIMER_VAL -31250 void timer_setup(void) { T0CONbits.TMR0ON = 0; // stop the timer T0CONbits.T08BIT = 0; // timer configured as 16-bit T0CONbits.T0CS = 0; // use FOSC T0CONbits.PSA = 0; // use prescaler T0CONbits.T0PS = 0b111; // prescaler 1:256 TMR0 = TIMER_VAL; T0CONbits.TMR0ON = 1; // start the timer INTCONbits.T0IF = 0; // reset timer interrupt flag INTCONbits.T0IE = 1; // enable timer interrupts RCONbits.IPEN = 0; // no priorities INTCONbits.PEIE = 1; // enable peripheral interrupts INTCONbits.GIE = 1; // enable interrupts globally } ¦ ¥ Corrado Santoro Some Exercises with Timers and UART
  • 7. Timer Task Two variables: light status, boolean, indicates the status of the light tick count, integer (8 bit), incremented in the timer ISR in order to count “timeouts” of 500 ms § unsigned char light_status, tick_count; // 8 bit vars #define ON 0 #define OFF 1 void interrupt isr(void) { if (INTCONbits.T0IF == 1) { TMR0 = TIMER_VAL; if (light_status == 0) { LIGHT = OFF; SIGNAL = OFF; tick_count = 0; } else { // ... see next slide } INTCONbits.T0IF = 0; } } ¦ ¥ Corrado Santoro Some Exercises with Timers and UART
  • 8. Timer Task (2) § ... else { // light_status == 1 LIGHT = ON; tick_count++; if (tick_count = 20) { // 20*500 = 10 sec light_status = 0; LIGHT = OFF; SIGNAL = OFF; } else if (tick_count = 10) { // 10*500 = 5 sec SIGNAL = !SIGNAL; // flashing } } ... ¦ ¥ Corrado Santoro Some Exercises with Timers and UART
  • 9. Task 2: “User interface” and Main Program § void main(void) { TRISAbits.TRISA3 = 1; // input TRISAbits.TRISA2 = 1; // input TRISBbits.TRISB0 = 0; // output TRISBbits.TRISB5 = 0; // output light_status = 0; tick_count = 0; timer_setup(); for (;;) { if (PB1 == ON) { // turn on the light and re-arm timer light_status = 1; tick_count = 0; } if (PB2 == ON) { // turn off the light light_status = 0; } } } ¦ ¥ Corrado Santoro Some Exercises with Timers and UART
  • 10. Exercise 2: A light with configurable timer We want to add to the previous program the ability to configure timeouts using some commands sent through the UART: The sequence “***”+CR enters configuration mode Two commands: timeout VAL, sets the “light off” timeout signal VAL, sets the timer value for the flashing signal end, exits configuration mode The program is a little bit more complex! Corrado Santoro Some Exercises with Timers and UART
  • 11. First let’s add timer variables § unsigned char off_timer = 20, signal_timer = 10; ... else { // light_status == 1 LIGHT = ON; tick_count++; if (tick_count = off_timer) { light_status = 0; LIGHT = OFF; SIGNAL = OFF; } else if (tick_count = signal_timer) { SIGNAL = !SIGNAL; // flashing } } ... ¦ ¥ Corrado Santoro Some Exercises with Timers and UART
  • 12. UART Setup § void uart_setup(void) { TRISCbits.TRISC6 = 0; // TX as output TRISCbits.TRISC7 = 1; // RX as input TXSTA1bits.SYNC = 0; // Async operation TXSTA1bits.TX9 = 0; // No tx of 9th bit TXSTA1bits.TXEN = 1; // Enable transmitter RCSTA1bits.RX9 = 0; // No rx of 9th bit RCSTA1bits.CREN = 1; // Enable receiver RCSTA1bits.SPEN = 1; // Enable serial port // Setting for 19200 BPS BAUDCON1bits.BRG16 = 0; // Divisor at 8 bit TXSTA1bits.BRGH = 0; // No high-speed baudrate SPBRG1 = 51; // divisor value for 19200 } ¦ ¥ Corrado Santoro Some Exercises with Timers and UART
  • 13. UART character reading, blocking and non-blocking § char read_char(void) { while (PIR1bits.RC1IF == 0) { // wait for char if (RCSTA1bits.OERR == 1) { RCSTA1bits.OERR = 0; // clear overrun if it occurs RCSTA1bits.CREN = 0; RCSTA1bits.CREN = 1; } } return RCREG1; } int nb_read_char(char * c_ptr) // non blocking version { if (RCSTA1bits.OERR == 1) { // check for errors in anycase RCSTA1bits.OERR = 0; // clear overrun if it occurs RCSTA1bits.CREN = 0; RCSTA1bits.CREN = 1; } if (PIR1bits.RC1IF == 0) { return 0; // no char available } else { // get char *c_ptr = RCREG1; return 1; } } ¦ ¥ Corrado Santoro Some Exercises with Timers and UART
  • 14. UART string reading and character output § void putch(char c) { // wait the end of transmission while (TXSTA1bits.TRMT == 0) {}; TXREG1 = c; // send the new byte } void read_line(char * s) { for (;;) { char c = read_char(); if (c ’ ’) { // it is a control char if (c == 0x0d) { // CR putch(13); *s = 0; return; } } else { // it is a printable char putch(c); *s = c; ++s; } } } ¦ ¥ Corrado Santoro Some Exercises with Timers and UART
  • 15. Exercise 2: Main Program § void main(void) { int asterisk_count = 0; TRISAbits.TRISA3 = 1; // input TRISAbits.TRISA2 = 1; // input TRISBbits.TRISB0 = 0; // output TRISBbits.TRISB5 = 0; // output light_status = 0; tick_count = 0; timer_setup(); uart_setup(); for (;;) { char c; if (PB1 == ON) { // turn on the light and re-arm timer light_status = 1; tick_count = 0; } if (PB2 == ON) // turn off the light light_status = 0; if (nb_read_char(c) == 1) { // a char is got if (c == ’*’) { ++asterisk_count; if (asterisk_count == 3) { enter_config(); asterisk_count = 0; } } else asterisk_count = 0; } } } ¦ ¥ Corrado Santoro Some Exercises with Timers and UART
  • 16. Exercise 2: Configuration Function § void enter_config(void) { char command[40]; printf(CONFIG); for (;;) { read_line(command); if (strcmp(command,end) == 0) { printf(END OF CONFIGURATIONn); return; } else if (strncmp(command,timeout,7) == 0) off_timer = atoi(command+7) * 2; else if (strncmp(command,signal,6) == 0) signal_timer = atoi(command+6) * 2; else printf(Invalid commandn); } } ¦ ¥ Corrado Santoro Some Exercises with Timers and UART
  • 17. Some Exercises with Timers and UART Corrado Santoro ARSLAB - Autonomous and Robotic Systems Laboratory Dipartimento di Matematica e Informatica - Universit`a di Catania, Italy santoro@dmi.unict.it L.A.P. 1 Course Corrado Santoro Some Exercises with Timers and UART