C Examples 1. Download Links MPLAB IDE dsPIC30F4011/4012 Data Sheet dsPIC30F Family Reference Manual MikroC MikroC Manual MikroC Quick Reference.

Slides:



Advertisements
Similar presentations
Review: Interrupts & Timers
Advertisements

4-1 Timers Timers can be used for  timing  event counting  pulse width measurement  pulse generation  frequency multiplication There are 8 Timers.
Microcontroller Programming II MP6-1
C Examples 1.
C Examples 4.
Introduction of Holtek HT-46 series MCU
Debouncing Switches Mechanical switches are one of the most common interfaces to a uC. Switch inputs are asynchronous to the uC and are not electrically.
8-Bit Timer/Counter 0 Counter/Timer 0 and 2 (TCNT0, TCNT2) are nearly identical. Differences: -TCNT0 can run off an external 32Khz clock (Tosc) or the.
5-1 Timer/Counters In many microprocessor systems we need to:  count logic pulses  measure the frequency of signals  measure the period of pulses 
Timers and Interrupts Shivendu Bhushan Summer Camp ‘13.
16-Bit Timer/Counter 1 and 3 Counter/Timer 1,3 (TCNT1, TCNT3) are identical in function. Three separate comparison registers exist. Thus, three separate.
1 Chapter 4 Timer Operation (I. Scott MacKenzie).
ECE 371- Unit 11 Timers and Counters (“Stop Watches and Alarm Clocks”)
7/23 Timers in Coldfire Processor Computer Science & Engineering Department Arizona State University Tempe, AZ Dr. Yann-Hang Lee (480)
V 0.91 Polled IO versus Interrupt Driven IO Polled Input/Output (IO) – processor continually checks IO device to see if it is ready for data transfer –Inefficient,
ARM Timers.
System Clocks.
Introduction to Using MPLAB and Hi-tide Group #9 JoseAlex MikeRoberto Goto and print the file
Timers ELEC 330 Digital Systems Engineering Dr. Ron Hayne
CoE3DJ4 Digital Systems Design Chapter 4: Timer operation.
Timers The timers of the PIC16C7X microcontroller can be briefly described in only one sentence. There are three completely independent timers/counters.
MCU: Interrupts and Timers Ganesh Pitchiah. What’s an MCU ?
RS232 #use rs232(baud=9600,xmit=PIN_C6,rcv=PIN_C7
1 Lab 1: Introduction. 2 Configure ATMEL AVR Starter Kit 500 (STK500), a prototyping/development board for Lab1. ATmega16 ( V) is the chip used.
Lecture 11: TI MSP430 Timers Compare Modes
© 2009, Renesas Technology America, Inc., All Rights Reserved 1 Course Introduction  Purpose This course provides an introduction to the peripheral functions.
C Examples 5. Download Links MPLAB IDE dsPIC30F4011/4012 Data Sheet dsPIC30F Family Reference Manual MikroC MikroC Manual MikroC Quick Reference.
Timers and Interrupts Anurag Dwivedi. Let Us Revise.
Saxion University of Applied Sciences Advanced Microcontrollers A practical approach.
Interrupts  An interrupt is any service request that causes the CPU to stop its current execution stream and to execute an instruction stream that services.
DsPIC30F4011 Fall DIP Switches  The upper four switches of SW1 are used to enable LEDs connected to PORTB/C, PORTA/D, PORTE and PORTF. For example,
© 2009, Renesas Technology America, Inc., All Rights Reserved 1 Course Introduction  Purpose This course provides an introduction to the peripheral functions.
Chapter 5 - Interrupts.
The Silicon Laboratories C8051F020
C Examples 1. Download Links dsPIC30F4011/4012 Data Sheet dsPIC30F4013/3014 dsPIC30F Family Reference Manual MikroC MikroC Manual MikroC Quick Reference.
8051 Aula 02 Prof Afonso Ferreira Miguel. Microcontrolador 8051 (MCS-51)  Timers do MCS51 Two 16-bit Counter/Timers: Two 16-bit Counter/Timers: Up counters,
Arduino Mega Arduino Mega 2560 Arduino Mega 2560.
PIC16F887.
C Examples 6. Download Links MPLAB IDE dsPIC30F4011/4012 Data Sheet dsPIC30F Family Reference Manual MikroC MikroC Manual MikroC Quick Reference.
Microcontrollers JULES CALELLA. Microcontrollers vs Microprocessors  Microprocessors – do not contain RAM, ROM, I/O  Microcontrollers – The whole package.
1 Lab 4: D/A Converter Lab 4: D/A Converter This is a simple resistive network for a D/A converter Port 1, Port 0 are digital inputs ==> 00 (minimum),
One more PIC18F252 example and revision for exam B222L Branislav Vuksanovic, UoP, ECE.
Microprocessors A practical approach..
TIMERS.
C Examples 3. Download Links dsPIC30F4011/4012 Data Sheet dsPIC30F4013/3014 dsPIC30F Family Reference Manual MikroC MikroC Manual MikroC Quick Reference.
C Examples 3. Download Links dsPIC30F4011/4012 Data Sheet dsPIC30F4013/3014 dsPIC30F Family Reference Manual MikroC MikroC Manual MikroC Quick Reference.
16-Bit Timer/Counter 1 and 3
Timer modules in PIC 16F877.  The PIC 16F877 basically has three independent timer modules,  denoted by the symbols, TIMER-0, TIMER1,andTIMER2. . These.
Why are Timer Functions Important?
Outline Introduction to Arduino UNO Programming environment setup GPIO
Application Case Study Security Camera Controller
Timers and Event Counters
PWM and DC Motor Control
AVR Addressing Modes Subject: Microcontoller & Interfacing
Microprocessors Timers
8-Bit Timer/Counter 0 Counter/Timer 0 and 2 (TCNT0, TCNT2) are nearly identical. Differences: -TCNT0 can run off an external 32Khz clock (Tosc) or the.
The Arduino Microcontroller: Atmel AVR Atmega 328
Timer/Counter Modified from Dr. Lam Phung’s Slides.
Pugazhendhi.M 11/12/2018 TIMERS 11/12/2018 CCET CCET.
Timer.
Timer Source: under
PIC18 Timer Programming “Explain the assembly language programming for the timer and counter module”
EECE.3170 Microprocessor Systems Design I
8051 Micro Controller.
EECE.3170 Microprocessor Systems Design I
Timer Source: under
ANALOG TO DIGITAL CONVERTER (ATD).
Timers Chapter 2.
Presentation transcript:

C Examples 1

Download Links MPLAB IDE dsPIC30F4011/4012 Data Sheet dsPIC30F Family Reference Manual MikroC MikroC Manual MikroC Quick Reference

This step necessary if you are using an RS-232 programmer.

This is necessary for the program to run after successfully programming

LED ON main () { TRISB=0b ; while(1) { PORTB.F1=1; }

Rapid40iXL I am using LED 1 connected to RB1 and SW4 connected to MCLR

Blinking LED - 1 main () { ADPCFG = 0xFFFF; //Analog input pin in Digital mode, // port read input enabled, TRISB=0b ; PORTB.F1=1; while(1) { Delay_ms(1000); PORTB.F1=PORTB.F1^1; }

Blinking LED - 2 main () { ADPCFG = 0xFFFF; TRISB=0b ; PORTB=PORTB|0b ; while(1) { Delay_ms(1000); PORTB=PORTB^0x02; }

Logical VS Bitwise

Timer 1 16-bit Timer Mode: In the 16-bit Timer mode, the timer increments on every instruction cycle up to a match value, preloaded into the Period register, PR1, then resets to 0 and continues to count. When the CPU goes into the Idle mode, the timer will stop incrementing unless the TSIDL (T1CON ) bit 0. If TSIDL 1, the timer module logic will resume the incrementing sequence upon termination of the CPU Idle mode. 16-bit Synchronous Counter Mode: In the 16-bit Synchronous Counter mode, the timer increments on the rising edge of the applied external clock signal, which is synchronized with the internal phase clocks. The timer counts up to a match value preloaded in PR1, then resets to 0 and continues. When the CPU goes into the Idle mode, the timer will stop incrementing unless the respective TSIDL bit o. If TSIDL 1, the timer module logic will resume the incrementing sequence upon termination of the CPU Idle mode.

Blinking LED - 3 main () { T2CON=0x8030; // Enable Timer 2 Prescaler=256 IEC0=0x0040; // Enable Interrupt for Timer 2 PR2=0xF424; ADPCFG = 0xFFFF; TRISB=0b ; PORTB=PORTB|0b ; while(1) { } void interrupt_T2() org 0x { PORTB=PORTB^0x02; IFS0=0x0000; }

Blinking LED - 4 main () { T2CON=0b ; // Enable Timer 2/3 IEC0=0x0080; // Enable Interrupt for Timer 3 PR2=0x6800; PR3=0x0989; ADPCFG = 0xFFFF; TRISB=0b ; PORTB=0x00; while(1) { } void interrupt_T2() org 0x { PORTB=PORTB^0x02; IFS0=0x0000; }

Switch + LED (SWITCH1) main () { ADPCFG = 0xFFFF; TRISB=0b ; TRISE=0b000001; PORTB=0x00; // THE CODE DOES NOT USE PORT B PORTE=0x1F; while(1) { if((PORTE&0b000001)==0) {PORTE.F1=PORTE.F1^1;} } Now I am using LED 4 connected to RB1 and SW1 connected to RE0

Key DEBOUNCING

Key DEBOUNCING – 1 (SWITCH2) main () { int i; const int Twentyms = 29,091; ADPCFG = 0xFFFF; TRISB=0b ; TRISE=0b000001; PORTB=0x00; PORTE=0x1F; while(1) { i = 0; // Wait 20 ms for Button Up while (i < Twentyms) { if (0 == PORTE.F0) // Button Down/Start over { i = 0; } else // Button Up/Increment Count { i = i + 1; } // i = 0; // Wait 20 ms for Button Down while (i < Twentyms) if (1 == PORTE.F0) // Button Up/Start over i = 0; else // Button Down/Increment Count i = i + 1; PORTE.F1=PORTE.F1^1; // Toggle LED to Turn ON/OFF LED } Adapted from 123 PIC Experiments for the EVIL GENIOUS by MYKE PREDKO

Key DEBOUNCING - 1 ;SWITCH2.c,13 :: while (i < Twentyms) $0120$L_main_2: $0120$470060ADDW14, #0, W0 $0122$780110MOV[W0], W2 $0124$210721MOV#4210, W1 $0126$510001SUBW2, W1, W0 $0128$3D000DBRA GEL_main_3, L_main_3 ;SWITCH2.c,15 :: if (0 == PORTE.F0) // Button Down/Start over $012A$8016D0MOVPORTE, W0 $012C$6000E1ANDW0, #1, W1 $012E$108060SUBRW1, #0, W0 $0130$3A0004BRA NZL_main_4, L_main_4 ;SWITCH2.c,17 :: i = 0; $0132$200000MOV#0, W0 $0134$780F00MOVW0, [W14] ;SWITCH2.c,18 :: } $0136$040140GOTOL_main_5 $013A$L_main_4: ;SWITCH2.c,21 :: i = i + 1; $013A$200011MOV#1, W1 $013C$470060ADDW14, #0, W0 $013E$408810ADDW1, [W0], [W0] ;SWITCH2.c,22 :: } // $0140$L_main_5: ;SWITCH2.c,23 :: } // $0140$040120GOTOL_main_2

Key DEBOUNCING – 2 (SWITCH3) int function_key(void) { int i,m; const int Twentyms = 29091; m=1; // SWITCH UP INDICATOR if (1 == PORTE.F0) {return(m);} while(1) { i = 0; // Wait 20 ms for Button Up while (i < Twentyms) { if (0 == PORTE.F0) // Button Down/Start over { i = 0; } else // Button Up/Increment Count { i = i + 1; } // m=0; // SWITCH DOWN INDICATOR return(m); }

Key DEBOUNCING - 2 main () { int s=1; ADPCFG = 0xFFFF; TRISB=0b ; PORTB=0x00; TRISE=0b000001; PORTE=0x1F; while(1) { s=function_key(); if (s==0) PORTE.F1=PORTE.F1^1; S=1; }

Key DEBOUNCING – 3 (SWITCH4) main () { int s=1; ADPCFG = 0xFFFF; TRISB=0b ; PORTB=0x00; TRISE=0b000001; PORTE=0x1F; while(1) { Delay_ms(5000); PORTB=~PORTB; s=function_key(); if (s==0) PORTE.F1=PORTE.F1^1; S=1; }

Key DEBOUNCING – 4 (SWITCH5 main () { int s=1; T2CON=0x8030; // Enable Timer 2 Prescaler=256 IEC0=0x0040; // Enable Interrupt for Timer 2 PR2=0xFFFF; ADPCFG = 0xFFFF; TRISB=0b ; PORTB=0x00; TRISE=0b000001; PORTE=0x1F; while(1) { s=function_key(); if (s==0) PORTE.F1=PORTE.F1^1; S=1; } void interrupt_T2() org 0x { PORTB=PORTB^0x02; IFS0=0x0000; }

UART1 U1MODE = 0x8400;

UART1 unsigned rx1; unsigned char uc1; void main() { Uart1_Init(19200); U1MODE = 0x8400; delay_ms(200); TRISE=0b000001; PORTE=0x1F; Uart1_Write_Char('a'); while(1) { if (Uart1_Data_Ready()) { rx1 = Uart1_Read_Char(); Uart1_Write_Char(++rx1); PORTE=~PORTE; } }//~!

UART2 unsigned rx1; unsigned adcRes; unsigned char uc1; void main() { PORTB = 0x0000; TRISB = 0xFFFF; Uart1_Init(19200); U1MODE = 0x8400; delay_ms(200); TRISE=0b000001; PORTE=0x1F; Uart1_Write_Char('a'); while(1) { adcRes = Adc_Read(3); adcRes = adcRes/4; Uart1_Write_Char(adcRes); PORTE=~PORTE; delay_ms(1000); } }//~!

PWM1 U1STA = 0x8400;

PWM1

int m,p; signed char n; main () { IEC0=0x0200; // Enable Interrupt for UART 1 Rx U1MODE = 0x8400; U1STA = 0x8400; U1BRG = 51; // BRG=(( /19200)/16)-1 Delay_ms(100); TRISB=0b ; PORTB.F1=0; m=Pwm_Mc_Init(40000,1,0xFF,0x00); Pwm_MC_Set_Duty(m,2); Pwm_MC_Set_Duty(m,1); Pwm_Mc_Start(); while(1) { Delay_ms(1000); PORTB.F1=0; }

m=Pwm_Mc_Init(40000,1,0xFF,0x00);

PWM1 void interrupt_UART1() org 0x { PORTB.F1=1; n=U1RXREG&0x07; switch (n) { case 1: p=-2; goto FLAG; case 2: p=-1; goto FLAG; case 3: p=1; goto FLAG; case 4: p=2; goto FLAG; default : goto END; FLAG: p=m+(m*p)/2; Pwm_MC_Set_Duty(p,1); Pwm_MC_Set_Duty(p,2); END: IFS0=0x0000; }

PWM2 int m,p; signed char n; main () { IEC0=0x8200; // Enable Interrupt for UART 1 Rx U1MODE = 0x8400; U1STA = 0x8400; U1BRG = 51; // BRG=(( /19200)/16)-1 CNEN1=0x0020; Delay_ms(100); ADPCFG = 0xFFFF; TRISB=0b ; PORTB.F1=0; m=Pwm_Mc_Init(40000,1,0xFF,0x00); Pwm_MC_Set_Duty(m,2); Pwm_MC_Set_Duty(m,1); Pwm_Mc_Start(); while(1) { Delay_ms(1000); PORTB.F1=0; }

PWM2

void interrupt_UART1() org 0x { PORTB.F1=1; n=U1RXREG&0x07; switch (n) { case 1: p=-2; goto FLAG; case 2: p=-1; goto FLAG; case 3: p=1; goto FLAG; case 4: p=2; goto FLAG; default : goto END; FLAG: p=m+(m*p)/2; Pwm_MC_Set_Duty(p,1); Pwm_MC_Set_Duty(p,2); END: IFS0=IFS0&0b ; } void interrupt_CN() org 0x { Pwm_MC_Set_Duty(0,1); Pwm_MC_Set_Duty(0,2); IFS0=IFS0&0b ; }

Low Frequency PWM The Indicated project settings, together with the code bellow were able to produce a 50 Hz PWM signal. int m; main () { ADPCFG = 0xFFFF; TRISE=0b ; TRISF=0b ; PORTE.F3=1; PORTB.F0=1; m=Pwm_Mc_Init(50,1,0xFF,0x00); Pwm_MC_Set_Duty(m,1); Pwm_MC_Set_Duty(m,2); Pwm_Mc_Start(); while(1) { PORTF.F0=1; }