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.

Using ADC n PWM to light up the LED dimmer - need help

Status
Not open for further replies.
Re: i am doing ADC

i know that to measure is t use oscillograph but the problem is that i don't know how to write code.. reading datasheet is very big different compare to write a programming code and i don't know how to write. i facing problem is that i don't know how to generate PWM i have a code with mi.. take a look and give mi some guide. thank



#include <C8051F200.h> // SFR declarations

//-----------------------------------------------------------------------------
// Global Constants
//-----------------------------------------------------------------------------

#define SYSCLK 16000000/8 // SYSCLK in Hz (16 MHz internal
// oscillator / 8)
// the internal oscillator has a
// tolerance of +/- 20%

#define TIMER_PRESCALER 12 // Based on Timer CKCON settings

// There are SYSCLK/TIMER_PRESCALER timer ticks per second, so
// SYSCLK/TIMER_PRESCALER/1000 timer ticks per millisecond.
#define TIMER_TICKS_PER_MS SYSCLK/TIMER_PRESCALER/1000

// Note: TIMER_TICKS_PER_MS should not exceed 255 (0xFF) for the 8-bit timer

#define AUX1 TIMER_TICKS_PER_MS
#define AUX2 -AUX1

#define LED_TOGGLE_RATE 100 // LED toggle rate in milliseconds
// if LED_TOGGLE_RATE = 1, the LED will
// be on for 1 millisecond and off for
// 1 millisecond

#define TIMER0_RELOAD_HIGH AUX2 // Reload value for Timer0 high byte

sbit LED = P2^4; // LED='1' means ON
sbit pwm = P1^3;
//-----------------------------------------------------------------------------
// Function Prototypes
//-----------------------------------------------------------------------------

void Port_Init (void); // Port initialization routine
void Timer0_Init (void); // Timer0 initialization routine

//-----------------------------------------------------------------------------
// main() Routine
//-----------------------------------------------------------------------------

unsigned int value;

void main (void)
{
WDTCN = 0xDE; // Disable watchdog timer
WDTCN = 0xAD;

Timer0_Init (); // Initialize the Timer0
Port_Init (); // Init Ports

EA = 1; // Enable global interrupts

while (1); // Loop forever
}

void Port_Init (void)
{
PRT2CF = 0x10; // Set LED to push-pull
}

void Timer0_Init(void)
{
TH0 = TIMER0_RELOAD_HIGH; // Reinit Timer0 High register
TL0 = TH0; // Set the intial Timer0 value

ET0 = 1; // Timer0 interrupt enabled
TMOD = 0x02; // 16-bit Mode Timer0
TCON = 0x10; // Timer0 ON
}


--
void Timer0_ISR (void) interrupt 1
{
static int counter = 0;

if((counter++) == LED_TOGGLE_RATE)
{
LED = ~LED; // Toggle the LED
pwm = ~pwm;
counter = 0;
}
}

//-----------------------------------------------------------------------------
// End Of File
//-----------------------------------------------------------------------------
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top