Re: i am doing ADC
i am doing ADC and PWM to generate a LED dim.. anyone can help mi? urgent
what does the below code do?
#include<c8051f200.h>
//definitions
sbit LED = P2^4;
void adc_init(); //to initialize adc
void port_init(); //to initialize the ports
void main()
{
unsigned int index=0, total_period= (unsigned int) 255*256, on_time=0;
port_init();
adc_init();
// disable watchdog timer
WDTCN = 0xde;
WDTCN = 0xad;
{
ADBUSY = 0x01; //starts the adc conversion
while(ADCINT); //polling for ADCINT which tells us the end of conversion
ADCINT = 0x00;
on_time = ((unsigned int)ADC0H)*256;
for ( index = 0 ; index < total_period ; index++)
{
if (on_time--)
{
LED = 0x01; //switching on the LED
}
else
{
LED = 0x00; //switching off the LED
}
}
}
}
void adc_init ()
{
AMX0SL = 0x3f; // enable AMUX & configure for p3.7 as i/p pin
ADC0CF = 0x60; // set conversion clk at one sys clk and PGA at gain =1
ADC0CN = 0xC0; // ADC is enabled and ADBUSY=0 (changed here)
REF0CN = 0x03; // VDD is selected as Vref. Here are you using VDD for variable resistor also?
}
void port_init()
{
P3MODE = 0X7F; // Except the P3.7 pin all are configured as digital I/O
}