First working version of PWM.

This commit is contained in:
Marvin Blum
2017-03-30 16:29:06 +02:00
parent 48d96d5b7d
commit 9f58ccfd83
3 changed files with 22 additions and 14 deletions

View File

@@ -137,21 +137,22 @@ unsigned int analog_read(unsigned char pin){
} }
// TODO: write PWM to digital pins which allow it // TODO: write PWM to digital pins which allow it
void analog_write(unsigned char pin, unsigned int value){ void analog_write(unsigned char pin, unsigned char value){
if(pin > 11){ if(pin != 3 && pin != 5 && pin != 6 && pin != 9 && pin != 10 && pin != 11){
return; return;
} }
if(value > 255){ // TODO maybe set pin mode?
value = 255;
}
/*if(value == HIGH){ // clear timer on compare match
PORTC |= _BV(pin); TCCR0A |= _BV(COM0A1)|_BV(WGM01)|_BV(WGM00);
}
else{ // set interrupt on overflow and value to compare to
PORTC &= ~_BV(pin); TIMSK0 |= _BV(TOIE0);
}*/ OCR0A = value;
// set timer scaling to 1, which starts the timer
TCCR0B |= _BV(CS00)|_BV(CS02);
} }
// maps A0-A7 to 0-7 // maps A0-A7 to 0-7
@@ -167,3 +168,8 @@ unsigned char map_analog_pin(unsigned char pin){
ISR(ADC_vect){ ISR(ADC_vect){
// required but not used... // required but not used...
} }
// fast PWM overflow interrupt handler
ISR(TIMER0_OVF_vect){
}

View File

@@ -19,6 +19,6 @@ void pin_mode(unsigned char, unsigned char);
int digital_read(unsigned char); int digital_read(unsigned char);
void digital_write(unsigned char, unsigned char); void digital_write(unsigned char, unsigned char);
unsigned int analog_read(unsigned char); unsigned int analog_read(unsigned char);
void analog_write(unsigned char, unsigned int); void analog_write(unsigned char, unsigned char);
#endif #endif

6
main.c
View File

@@ -33,9 +33,11 @@ void prepare(){
// PWM example // PWM example
pin_mode(A1, INPUT); pin_mode(A1, INPUT);
pin_mode(3, OUTPUT); pin_mode(6, OUTPUT);
} }
unsigned char pwm = 0;
void loop(){ void loop(){
// digital input/output example // digital input/output example
/*if(digital_read(10) && digital_read(8)){ /*if(digital_read(10) && digital_read(8)){
@@ -54,7 +56,7 @@ void loop(){
// PWM example // PWM example
int analog = map(analog_read(A1), 0, 1023, 0, 255); int analog = map(analog_read(A1), 0, 1023, 0, 255);
analog_write(3, analog); analog_write(6, analog);
char out[15]; char out[15];
sprintf(out, "%d", analog); sprintf(out, "%d", analog);