UKHAS Wiki

UK High Altitude Society

User Tools

Site Tools


code:ground_control

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
code:ground_control [2008/11/13 00:39] laurencebcode:ground_control [2008/11/13 09:50] (current) laurenceb
Line 1: Line 1:
 ===== Input capture based version ===== ===== Input capture based version =====
 This uses the input capture function on timer1 (atmega168). The ICNC1 bit in the TCCR1B register need to be set to enable oversampling to give more noise tolerance. A second function running approx every 20ms is needed to decrement counter by two and check if counter>=30. If this is the case there is a valid pwm stream and the diff variable can be used to set the pwm output.  This uses the input capture function on timer1 (atmega168). The ICNC1 bit in the TCCR1B register need to be set to enable oversampling to give more noise tolerance. A second function running approx every 20ms is needed to decrement counter by two and check if counter>=30. If this is the case there is a valid pwm stream and the diff variable can be used to set the pwm output. 
- 
- 
  
 ==== Header ==== ==== Header ====
 <code c> <code c>
-#define enable_ground_control TIMSK1|=(1<<ICIE1) //enables the input capture interrupt +void enable_ground_control(); //enables the input capture interrupt 
-#define disable_ground_control TIMSK1&=~(1<<ICIE1) //disables input capture interrupt+void disable_ground_control(); //disables input capture interrupt 
 +#define PULSE_MIN (s16)(0.001*(float)F_CPU/16.0) //pwm must be in the range 1 to 2 ms 
 +#define PULSE_MAX (s16)(0.002*(float)F_CPU/16.0) //due to the bit shift we divide by 16 
 +#define GROUND_LED_ON PORTD|=(1<<3) 
 +#define GROUND_LED_OFF PORTD&=~(1<<3) 
 +#define GROUND_LED_SET PORTD&(1<<3) 
 +void run_ground_control(); 
 +</code>
  
-#define PULSE_MIN 0.001*F_CPU/8; //pwm must be in the range 1 to 2 ms 
-#define PULSE_MAX 0.002*F_CPU/8; 
-</code> 
  
 ====Main code=== ====Main code===
Line 19: Line 21:
 #include "main.h" #include "main.h"
 extern volatile u08 counter; //globals extern volatile u08 counter; //globals
-extern volatile s16 diff;+extern volatile u16 diff;
    
    
Line 35: Line 37:
  if(!(counter&0x80)) //the lock bit isn't set, so we are free to run  if(!(counter&0x80)) //the lock bit isn't set, so we are free to run
  {  {
- diff=(s16)(ICR1>>1)-(s16)(start>>1); //set the global volatile s16 diff + diff=ICR1; //set the global volatile s16 diff 
- if(start>ICR1) //our pwm pulse spans a timer overflow+ if(start>diff) //our pwm pulse spans a timer overflow
  {  {
- diff+=(s16)(TOP_COUNT>>1);+ diff+=TOP_COUNT;
  }  }
 + diff-=start;
  if(diff>PULSE_MIN && diff<PULSE_MAX) //test against criterion  if(diff>PULSE_MIN && diff<PULSE_MAX) //test against criterion
  {  {
Line 60: Line 63:
  {  {
  counter|=0x80; //lock the diff variable using the counter flag bit  counter|=0x80; //lock the diff variable using the counter flag bit
- OCR1B=((u16)diff)<<1;+ OCR1B=diff;
  counter&=~0x80;  counter&=~0x80;
  GROUND_LED_ON;  GROUND_LED_ON;
code/ground_control.1226536757.txt.gz · Last modified: 2008/11/13 00:39 by laurenceb

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki