pic18f- delay program using timer

Upload: veeramaniks408

Post on 02-Mar-2016

101 views

Category:

Documents


0 download

DESCRIPTION

Delay program - PIC18F family

TRANSCRIPT

PIC18F- Delay program using TIMER

#include #define _XTAL_FREG 20000000#pragma config PLLDIV = 4, CPUDIV = OSC1_PLL2, FOSC = HS, FCMEN = OFF, IESO = OFF, PWRT = OFF, BOR = ON, BORV = 3, WDT = OFF, WDTPS = 32768#pragma config MCLRE = ON, LPT1OSC = OFF, PBADEN = ON, CCP2MX = ON, STVREN = ON, LVP = OFF, XINST = OFF, VREGEN = OFF, ICPRT = OFF, DEBUG = ON#pragma config CP0 = OFF, CP1 = OFF, CP2 = OFF, CP3 = OFF, CPB = OFF, CPD = OFF, WRT0 = OFF, WRT1 = OFF, WRT2 = OFF, WRT3 = OFF#pragma config WRTB = OFF, WRTC = OFF, WRTD = OFF, EBTR0 = OFF, EBTR1 = OFF, EBTR2 = OFF, EBTR3 = OFF, EBTRB = OFF

void ms_delay(unsigned int x){/*Generate 1ms time delay1. crystal freq = 20MHZ20MHZ/4 = 5MHZ1/5MHZ = 200 ns2. Required time delay = 1ms1 ms/200 ns = 5,000if, 10 ms/200 ns = 50,000if, 100 ms/200 ns = 5,00,000 (for 16-bt reg, the max value will be 65535, so have to use prescalar, 1:16 / 1:32 , etc)prescalar set by TCON registerfor 1ms, TMRO value is 65535 - 5000 = 60535 = 0xEC77for 100ms, TMRO value is 65535 - 5,00,000/16 = 34285 = 0x85ED (with prescalar 1:16)3. Load TCON reg for timer0 enable and prescalar is not assined (PSA=1)TCON = 0x884.TMR0H = 0xEC and TMROL = 0x775. Load INTCONT reg. : Clear TMROIF and check the rolls over- TMROIF is set.*/unsigned int i;for (i=0;i