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.

[SOLVED] help with the code for 8051 microcontroller

Status
Not open for further replies.

fareen

Full Member level 3
Joined
Oct 31, 2010
Messages
154
Helped
4
Reputation
8
Reaction score
4
Trophy points
1,298
Activity points
2,193
actually this is project in which i will get pulses from optical encoder the 8051 uc will count the pulses and then will produced a modulated wave form acorrdingly
Code:
#include <REGX51.H>
 unsigned long ref_speed,read_counter, error, kp, t,dc,Ton;
 sbit outbit=P1^0;
 char i;
  main()
 {
  TMOD=0x15;
  IE=0x88;   //Enable timer 1 interrupt
  kp=1;
  t=100;
  ref_speed=800;
  TH1=255;
 TL1=240;
 TR1=1;
  
while (1)
{
error=0;
TH0=0;
TL0=0;
TR0=1;
for ( i=0;i<1000;i++);
TR0=0;
read_counter=TL0;
TR0=1;
error=ref_speed -( read_counter );
read_counter=read_counter*1000/15;
error=kp*error;
dc=50+error;
if (dc>=90)
dc=90;
elseif(dc<=10)
dc=10;
else
;
Ton=(dc*100);

}
}

 void t1isr(void) interrupt 3
 {
 TR1=0;
 if (outbit==0)
 {outbit=!outbit;
 TH1=255;
 TL1=255-Ton;
 TR1=1;
 }
 else 
 {outbit=!outbit;
 TH1=255;
 TL1=255-(100-Ton);
 TR1=1;
 }
 }
plz take a look and guide me im not getting the modulted waveform
 
Last edited:

actually this is project in which i will get pulses from optical encoder the 8051 uc will count the pulses and then will produced a modulated wave form acorrdingly
Code:
#include <REGX51.H>
 unsigned long ref_speed,read_counter, error, kp, t,dc,Ton;
 sbit outbit=P1^0;
 char i;
  main()
 {
  TMOD=0x15;
  IE=0x88;   //Enable timer 1 interrupt
  kp=1;
  t=100;
  ref_speed=800;
  TH1=255;
 TL1=240;
 TR1=1;
  
while (1)
{
error=0;
TH0=0;
TL0=0;
TR0=1;
for ( i=0;i<10;i++);
TR0=0;
read_counter=TL0;
TR0=1;
error=ref_speed -( read_counter );
read_counter=read_counter*1000/15;
error=kp*error;
dc=50+error;
if (dc>=90)
dc=90;
elseif(dc<=10)
dc=10;
else
;
Ton=(dc*100);

}
}

 void t1isr(void) interrupt 3
 {
 TR1=0;
 if (outbit==0)
 {outbit=!outbit;
 TH1=255;
 TL1=255-Ton;
 TR1=1;
 }
 else 
 {outbit=!outbit;
 TH1=255;
 TL1=255-(100-Ton);
 TR1=1;
 }
 }
plz take a look and guide me im not getting the modulted waveform

How it connected ?
And for modulated signal, explain more
 
Last edited:

Make that pin output first
Port 0 is an 8-bit open drain bidirectional I/O port.
didnt get it
modulated signal means chaniging its dutycycle
the PWM generated in this way will be use to drive a dc motor through h bridge
 

Which input pin getting the input and which output pin is connected to H-Bridge?
 

P1^0 connected to the output
and the timer0 pin P3^4 gets the input signal from optical encoder
 

P1^0 connected to the output
and the timer0 pin P3^4 gets the input signal from optical encoder

Thats what I tried to say in my previous post.
All ports of 8051 upon reset are configured as input. When 0 first written to it, it becomes an output.

and timer0 pin?

A little more details with the circuit diagram will be helpful.
 
Last edited:

P1_0 port is output port not input
and timer have an external pin in order to clk signal from any other source
if u see the pin diagram this will be clear
 

:) All I tried to say that If you want to use Timer/counter 0 or 1 external input pins(p3.4 or p3.5) only configuring TMOD register is not enough. You need to configure them as input.
 
Last edited:

P1_0 port is output port not input
and timer have an external pin in order to clk signal from any other source
if u see the pin diagram this will be clear

Its my kind and humble request to you sir, while we check the pin diagram, please try to remember the simple rule that input and output pins needs initialization.

And without knowing the crystal frequency used its not easy to tell the timer1 is properly initialized or not.
 

All I tried to say that If you want to use Timer/counter 0 or 1 external input pins(p3.4 or p3.5) not only configuring TMOD register is enough. You need to configure them as input.
you mean other than initialing TMOD i need to configure them as input also???
correct me if im wrong
we intialize the input port by sending them logic 1??
And without knowing the crystal frequency used its not easy to tell the timer1 is properly initialized or not.
crystal frequency is 12mhz
 

Yes, Before writing to TMOD register.
like,

T0=1 //to make T0 an input
TMOD=0x15; //timer 1 as timer and timer0 as counter

---------- Post added at 01:52 ---------- Previous post was at 01:40 ----------

Use timer1 as time base for timing 1 second. During 1 second, Timer0 should run as a counter and get input pulses from P3.4. At the end of 1 sec, TL0 and TH0 give you the number of pulses.

For 22MHz, The value for instruction cycle = 1.833Mhz and T=0.546us
For both TH and TL, it initialize with 00 then Maximum delay =65,536x0.546us=35.75ms. You need another loop for 28 times to get 1Sec delay.

In your program timer1 delay is 16x0.546us=8.736us.
Are you applying a different logic? What is it?

---------- Post added at 02:06 ---------- Previous post was at 01:52 ----------

Sorry your crystal frequency is 12MHz. Anyway calculate and check it you are using correct settings

---------- Post added at 02:12 ---------- Previous post was at 02:06 ----------

For 12MHz, The value for instruction cycle = 1Mhz and T=1us
For both TH and TL, it initialize with 00 then Maximum delay =65,536x1us=65.54ms.

In your program timer1 delay is 16x65.54ms=1048mS almost 1sec.
 

oh my bad actually for loop run for 1000 count
so it gives a delay of 1ms
i edit the it for sumthing and forget it to make it right
 

@fareen

I see no mention as to the actual manufacture and model of the 8051 variant used.

What is the manufacture and actual model number of the 8051 you are utilizing in your design?

Is this project a simulation or constructed in physical hardware?

BigDog
 

@big dog
its physical hardwre project and we are using atmel 89c51 microcontroller
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top