From e92b9a8e065396139c90207acdeadd3fb79e901c Mon Sep 17 00:00:00 2001 From: jaseg Date: Thu, 7 Jun 2012 00:02:38 +0200 Subject: [PATCH] Working on the software pwm code. --- cerebrum_firmware.c | 10 ++++++++++ config.c | 34 +++++++++++++++++++++++++--------- config.h | 3 ++- pwm.c | 33 +++++++++++++++++++++------------ pwm.h | 5 +++++ 5 files changed, 63 insertions(+), 22 deletions(-) diff --git a/cerebrum_firmware.c b/cerebrum_firmware.c index 8145842..9dab9da 100644 --- a/cerebrum_firmware.c +++ b/cerebrum_firmware.c @@ -41,6 +41,7 @@ void setup(){ char nbuf; char bpos; int state = 0; +uint8_t somecycle = 0; //FIXME debug value void loop(){ //one frame static uint8_t escape_state = 0; @@ -142,6 +143,8 @@ void loop(){ //one frame if(c > PWM_COUNT) c = 0; nbuf = c; + if(nbuf >= PWM_COUNT) + nbuf = 0; state = 6; break; case 6: @@ -171,4 +174,11 @@ void loop(){ //one frame input_loop(); pwm_loop(); _delay_ms(1); + somecycle++; + if(!somecycle){ + pwm_val[0]++; + pwm_val[1]++; + pwm_val[2]++; + pwm_val[3]++; + } } diff --git a/config.c b/config.c index 18de067..9c5272d 100644 --- a/config.c +++ b/config.c @@ -6,9 +6,25 @@ void config_setup(){ } +void pwm_output_setup(){ + DDRE |= 0x30; + DDRB |= 0xC0; +} + +void pwm_unset_outputs(){ + PORTE &= ~0x30; + PORTB &= ~0xC0; +} + +void pwm_set_outputs(uint8_t data){ + data <<= 4; + PORTE |= data&0x30; + PORTB |= data&0xC0; +} + //FIXME This is a monstrum. A hardware fix is urgently required. void led_output_stuff1(uint8_t data, uint8_t dir){ - // Q&0x01 ==> PG5 + // data&0x01 ==> PG5 // 02 ==> PE3 // 04 ==> PH3 // 08 ==> PH4 @@ -17,20 +33,20 @@ void led_output_stuff1(uint8_t data, uint8_t dir){ // 40 ==> PB4 // 80 ==> PB5 DDRG&=~(1<<5); - DDRG|=(DDRQ&1)<<5; + DDRG|=(dir&1)<<5; PORTG&=~(1<<5); - PORTG|=(Q&1)<<5; + PORTG|=(data&1)<<5; DDRE&=~(1<<3); - DDRE|=(DDRQ&2)<<2; + DDRE|=(dir&2)<<2; PORTE&=~(1<<3); - PORTE|=(Q&2)<<2; + PORTE|=(data&2)<<2; DDRH&=0xC3; - DDRH|=(DDRQ&0x3C); + DDRH|=(dir&0x3C); PORTH&=0xC3; - PORTH|=(Q&0x3C); + PORTH|=(data&0x3C); DDRB&=0xCF; - DDRB|=(DDRQ&0xC0)>>2; + DDRB|=(dir&0xC0)>>2; PORTB&=0xCF; - PORTB|=(Q&0xC0)>>2; + PORTB|=(data&0xC0)>>2; } diff --git a/config.h b/config.h index 4eac359..54a825b 100644 --- a/config.h +++ b/config.h @@ -2,12 +2,13 @@ #ifndef __CONFIG_H__ #define __CONFIG_H__ +void config_setup(void); + #define HAS_PWM_SUPPORT 1 #define HAS_R0KETBEAM_SUPPORT 1 //#define HAS_INPUT_SUPPORT 1 #define HAS_LED_SUPPORT 1 -#define PWM_COUNT 5 //#define INPUT_COUNT 1 diff --git a/pwm.c b/pwm.c index c881f9b..cc8e0be 100644 --- a/pwm.c +++ b/pwm.c @@ -1,5 +1,6 @@ #include "pwm.h" +#include "led.h" //The PWM has no loop function. It runs entirely out of the ISR. void pwm_loop(){} @@ -9,30 +10,38 @@ void pwm_loop(){} void pwm_setup(){ //s/w "BCM"(<== "Binary Code Modulation") timer setup TCCR0A |= _BV(WGM00)|_BV(WGM01); - TCCR0B |= _BV(CS02); - TIMSK0 |= _BV(TOIE0); - //FIXME set PWM output DDRs + TCCR0B |= _BV(CS01)|_BV(CS00)|_BV(WGM02); + //TIMSK0 |= _BV(TOIE0); + OCR0A = 1; + pwm_output_setup(); } //Software PWM stuff //Called every 256 cpu clks (i.e should not get overly long) ISR(TIMER0_OVF_vect){ + pwm_cycle<<=1; + if(!pwm_cycle){ + pwm_cycle = 1; + } uint8_t Q = 0; + if(pwm_cycle&0x80) + frameBuffer[0]^=1; + pwm_unset_outputs(); Q |= (pwm_val[0] & pwm_cycle); Q |= (pwm_val[1] & pwm_cycle)?2:0; Q |= (pwm_val[2] & pwm_cycle)?4:0; Q |= (pwm_val[3] & pwm_cycle)?8:0; - Q |= (pwm_val[4] & pwm_cycle)?16:0; - PORTC = Q; - OCR0A = pwm_cycle; - pwm_cycle<<=1; - if(!pwm_cycle){ - pwm_cycle = 1; - } + /*Q |= (pwm_val[4] & pwm_cycle)?16:0; + Q |= (pwm_val[5] & pwm_cycle)?32:0; + Q |= (pwm_val[6] & pwm_cycle)?64:0; + Q |= (pwm_val[7] & pwm_cycle)?128:0; + */ + pwm_set_outputs(Q); + OCR0A = 1;//pwm_cycle; } -uint8_t pwm_cycle = 0; -uint8_t pwm_val[PWM_COUNT]; +uint8_t pwm_cycle = 1; +uint8_t pwm_val[8]; #else//HAS_PWM_SUPPORT diff --git a/pwm.h b/pwm.h index 37433c4..044c9d3 100644 --- a/pwm.h +++ b/pwm.h @@ -12,6 +12,11 @@ extern uint8_t pwm_cycle; extern uint8_t pwm_val[]; +#define PWM_COUNT 8 + +extern void pwm_output_setup(void); +extern void pwm_set_outputs(uint8_t data); +extern void pwm_unset_outputs(void); #endif//HAS_PWM_SUPPORT -- 2.11.4.GIT