Professional Documents
Culture Documents
c
*
* Created: 9/28/2013 11:25:16 PM
*
*/
#define F_CPU 16000000UL
#include <avr/io.h>
#include <util/delay.h>
void InitPWM()
{
TCCR0|=(1<<WGM00)|(1<<WGM01)|(1<<COM01)|(0<<CS02)|(0<<CS01)|(1<<CS00);
TCCR1A|=(1<<COM1A1)|(1<<COM1B1)|(1<<WGM11);
TCCR1B|=(1<<WGM13)|(1<<WGM12)|(0<<CS12)|(0<<CS11)|(1<<CS10);
ICR1=255;
DDRB|=(1<<PB3);
DDRD|=(1<<PD5)|(1<<PD4);
OCR0=0;
OCR1A=0;
OCR1B=0;
}
void Waiting(int j) // simple delay function
{
uint8_t i;
for(i=0;i<j;i++)
_delay_ms(200);
}
void SetADC()
{
ADMUX|=(1<<REFS0)|(1<<ADLAR); //8 bit conversion
ADCSRA=(1<<ADEN)|(7<<ADPS0);
}
uint16_t ReadADC(uint8_t ch)
{
//Select ADC Channel ch must be 0-7
ch=ch&0b00000111;
ADMUX&=0b11100000;
ADMUX|=ch;
//Start Single conversion
ADCSRA|=(1<<ADSC);
//Wait for conversion to complete
while(!(ADCSRA & (1<<ADIF)));
//Clear ADIF by writing one to it
ADCSRA|=(1<<ADIF);
return(ADCH); //only higher byte is returned
}
void main()
{
SetADC();
InitPWM();
while(1)
{
OCR0=ReadADC(1);
OCR1A=ReadADC(2);
OCR1B=ReadADC(3);
}
Acelore
#include<avr/io.h>
#include <util/delay.h>
#include<adc_lib.h>
#include<lcd.h>
void main()
{
int adc0;
ADCinit();
LCDinit();
LCDclr();
LCDcursorOFF();
char ch[16];
while(1)
{
adc0=read_adc(0);
sprintf(ch,"X: %d",adc0);
LCDGotoXY(0,0);
LCDdisplay(ch);
LCDsendChar(' ');
adc0=read_adc(1);
sprintf(ch,"Y: %d",adc0);
LCDGotoXY(0,10);
LCDdisplay(ch);
LCDsendChar(' ');
adc0=read_adc(2);
sprintf(ch,"Z: %d",adc0);
LCDGotoXY(1,5);
LCDdisplay(ch);
LCDsendChar(' ');
_delay_ms(500);
}
}
void t0_pwm_init(void);
int main()
{
t0_pwm_init();
sei(); // enable global interrupt
while(1);
}
void t0_pwm_init() //// intiatialize of timer0
{
// WGM0[1:0]= 01, for Phase Correct PWM mode
// COM0[1:0]= 10, to select non inveting mode
// CS0[2:0] =001. for no prescaler
TCCR0=(1<<WGM00)|(2<<COM00)|(1<<CS00);
DDRB|=(1<<PB3); // selcetOC0 as output pin
TIMSK|=(1<<OCIE0); //enable Output compare interrupt
}
140,548-Reads
Pick the right product for your next design project with "Mouser
Store". Mouser Electronics brings broadest range of semiconductors and
electronic components from over 500 industry leading suppliers.
Mouser specializes in the rapid introduction of new products and technologies for
design engineers and buyers that includes semiconductors, interconnects,
passives, and electro-mechanical components. Providing the best possible
service with the flexibility of not requiring a minimum order and same-day
shipping, Mouser has it all.
Temperature Sensors
(7,586)
Integrated Circuits
ICs (288,369)
Multimeters &
Voltmeters (813)
Resistors (332,210)
Potentiometers &
Trimmers (26,588)
Encoders (4,497)
Relays (32,934)
7 Segment Displays
(151)
Transformers (6,455)
Capacitors (354,282)
Proximity Sensors
(6,731)
Pressure Sensors
(10,544)
When you need the right part right now, think of Mouser
Check out the store now!
Thank you
Team EG