Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

making a pulse of 2 ms using timer in pic 18f452

Status
Not open for further replies.

linkstatic

Junior Member level 1
Joined
Apr 14, 2012
Messages
15
Helped
2
Reputation
4
Reaction score
2
Trophy points
1,283
Activity points
1,375
Code:
#pragma config OSC=HS,OSCS=ON
#pragma config WDT=OFF
#pragma config PWRT=ON,BOR=ON,BORV=45

#include<P18F452.h>
#define mybit PORTBbits.RB4
void delay(void);

void main()

{
TRISBbits.TRISB4=0;
while(1)
  {
mybit^=1;

delay();
}




}

void delay(void)
{
T0CON=0x05;
TMR0H=0xFF;
TMR0L=0xB2;
T0CONbits.TMR0ON=1;
while(INTCONbits.TMR0IF==0);
T0CONbits.TMR0ON=0;
INTCONbits.TMR0IF=0;
}

its not generating pulses of 2 ms am i doing something wrong here? is the code right?

---------- Post added at 21:00 ---------- Previous post was at 20:59 ----------

the oscillators frequency is taken as 10 MHz

---------- Post added at 21:21 ---------- Previous post was at 21:00 ----------

FFFFh-FFB2h=4Dh=77+1=78
78*64*0.4 us=1996.8 us=2 ms

---------- Post added at 21:22 ---------- Previous post was at 21:21 ----------

the prescale is 1:64 as T0CON=0x05 = 00000101
 

Code:
#pragma config OSC=HS,OSCS=ON
#pragma config WDT=OFF
#pragma config PWRT=ON,BOR=ON,BORV=45

#include<P18F452.h>
#define mybit PORTBbits.RB4
void delay(void);

void main()

{
TRISBbits.TRISB4=0;
while(1)
  {
mybit^=1;

delay();
}




}

void delay(void)
{
T0CON=0x05;
TMR0H=0xFF;
TMR0L=0xB2;
T0CONbits.TMR0ON=1;
while(INTCONbits.TMR0IF==0);
T0CONbits.TMR0ON=0;
INTCONbits.TMR0IF=0;
}

its not generating pulses of 2 ms am i doing something wrong here? is the code right?

---------- Post added at 21:00 ---------- Previous post was at 20:59 ----------

the oscillators frequency is taken as 10 MHz

---------- Post added at 21:21 ---------- Previous post was at 21:00 ----------

FFFFh-FFB2h=4Dh=77+1=78
78*64*0.4 us=1996.8 us=2 ms

---------- Post added at 21:22 ---------- Previous post was at 21:21 ----------

the prescale is 1:64 as T0CON=0x05 = 00000101

Apart from crystal frequency and prescaler factor, another factor in the delay size is the C compiler . :-D
 
Last edited:

so i can never get a 2 ms pwm? i need to run a servo :(
 

This website allows you to enter your PWM frequency and it will generate all the necessary code to initialise the PWM module for you.

http://www.micro-examples.com/public/microex-navig/doc/097-pwm-calculator.html

Or there is this website for generating the timer frequency.

**broken link removed**

In terms of your code above, what is the output actually doing, is it pulsing?
Do you actually need to use the timer to generate this delay because as your arent using interrupts your are still just burning CPU cycles waiting for the interrupt flag to be set, could you not use the delays.h library which takes the clock speed into acount and gives you accurate delays?

Hope this helps.

/Pheetuz
 
so i can never get a 2 ms pwm? i need to run a servo :(
2ms in microprocessor terms is an age, you will be able to get pretty damn close to 2mS, essentially to within one instruction cycle depending on your code.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top