I have been working on code for Atmega8, for controlling in 10 steps the speed of a n AC fan.
IR remote is used for varying the speed and triac is fired on zero crossing.
I have tested the code in sections ie The display part with IR (Increment/decrement).
On adding the code for zero cross detection and output to control gate signal.
The code does'nt compile
The code is followed with the compile error.
#include <EEPROM.h>
#include <boarddefs.h>
#include <IRremote.h>
#include <IRremoteInt.h>
#include <ir_Lego_PF_BitStreamEncoder.h>
#define setbit(port, bit) (port) |= (1<< (bit))
#define clearbit(port, bit) (port) &=~(1<< (bit))
#define POWER_LED_ON() clearbit(PORTD, PD7)
#define POWER_LED_OFF() setbit(PORTD, PD7)
uint8_t table[10] = {141, 125, 109, 94, 78, 62, 47, 31, 16, 1};
const int iRReceivePin = PD2;
IRrecv irrecv(iRReceivePin);
decode_results results;
/* iniialise speed to zero, and also store the set value in EEPROM
on every change to be read from it on power recycle.
*/
char speedVal = 0;
char num = 0;
uint8_t fan_on = 0;
uint8_t lastPowerState = 0;
void setup()
{
// put your setup code here, to run once:
initialize();
//Initialize the zero crossing detector INT(1)
MCUCR |= ((1 << ISC11) | (1 << ISC10)); //INT in Rising edge
GICR |= (1 << INT1); //Enable INT1
//Set Timer 1
TCCR1A = 0;
TCCR1B = 0;
TCCR1B |= (1 << WGM12); //CTC
TCCR1B |= ((1 << CS12) | (1 << CS10));
TIMSK |= (1 << OCIE1A); //Enable CTC Output capture
sei();
}
//Zero Crossing Detect.
ISR(INT1_vect)
{
if (!fan_on)
{
PORTD |= (1 << PD5); //High = TRIAC Off
return;
}
if (9 == speedVal)
{
PORTD &= (~(1 << PD5)); //low = TRIAC ON
return;
}
PORTD |= (1 << PD5); //High = TRIAC Off
OCR1A = table[speedVal];
TCNT1H = 0x00;
TCNT1L = 0x00;
TCCR1B |= ((1 << CS12) | (1 << CS10)); // eneble presccalar of 1024
}
ISR(TIMER1_COMPA_vect)
{
PORTD &= (~(1 << PD5)); //low = TRIAC ON
TCCR1B &= (~((1 << CS12) | (1 << CS10))); //Stop Timer
}
void loop()
{
while (1)
{
if (irrecv.decode(&results))
{
switch (results.value)
{
case 0xFF48B7: // NEC code for UP Key
{
if (speedVal < 9)
speedVal++;
Display (speedVal);
EEPROM.put(5, speedVal);
}
break;
case 0XFF609F: //NEC Code for DOWN Key
{
if (speedVal > 0)
speedVal--;
Display (speedVal);
EEPROM.put(5, speedVal);
}
break;
case 0xFFAA55: // NEC code for enter key
{
if (fan_on)
{
POWER_LED_OFF();
fan_on = 0;
lastPowerState = fan_on;
EEPROM.put (3, lastPowerState);
}
else
{
POWER_LED_ON();
fan_on = 1;
lastPowerState = fan_on;
EEPROM.put (3, lastPowerState);
}
}
break;
}
irrecv.resume(); // Receive the next value
}
}
}
/* Seven Segment Display Function */
void Display(char num)
{
if (num > 9)
return;
switch (num)
{
case 0:
PORTC = 0B00000000;
PORTB = 0B00000010;
break;
case 1:
PORTC = 0B00111001;
PORTB = 0B00000010;
break;
case 2:
PORTC = 0B00100100;
PORTB = 0B00000000;
break;
case 3:
PORTC = 0B00110000;
PORTB = 0B00000000;
break;
case 4:
PORTC = 0B00011001;
PORTB = 0B00000000;
break;
case 5:
PORTC = 0B00010010;
PORTB = 0B00000000;
break;
case 6:
PORTC = 0B00000010;
PORTB = 0B00000000;
break;
case 7:
PORTC = 0B00111000;
PORTB = 0B00000010;
break;
case 8:
PORTC = 0B00000000;
PORTB = 0B00000000;
break;
case 9:
PORTC = 0B00010000;
PORTB = 0B00000000;
break;
}
}
void initialize()
{
irrecv.enableIRIn(); // Start the IR receiver
/* PD7 as output */
POWER_LED_OFF();
DDRD |= (1 << PD7);
/* Set PD2 and PD3 as inputs */
DDRD &= ~(1 << PD2);
DDRD &= ~(1 << PD3);
// Port C pins 0 to 5 as output for seg a b c d e f
// Port B pin 1 as output for segment g
DDRC |= (1 << PC0) | (1 << PC1) | (1 << PC2) | (1 << PC3) | (1 << PC4) | (1 << PC5);
DDRB |= (1 << PB1);
/* Sets all segments a to g in the display off */
setbit (PORTC, PC0);
setbit (PORTC, PC1);
setbit (PORTC, PC2);
setbit (PORTC, PC3);
setbit (PORTC, PC4);
setbit (PORTC, PC5);
setbit (PORTB, PB1);
EEPROM.get (5, speedVal);
Display (speedVal);
EEPROM.get (3, lastPowerState);
fan_on = lastPowerState;
if (lastPowerState)
{
POWER_LED_ON();
}
else
{
POWER_LED_OFF();
}
/* Output - blocking gate signal to opto isolator */
setbit(PORTD, PD5); //PD5 High = Triac off
DDRD |= (1 << PD5); //set DDR for port BIT 5
}
The max character limit is exceeded with the compile error.
Try to post it in next message.
error message.txt (14.2 KB)