Dear All, I have been struggling with this circuit and code. my intention is to control scr phase angle with atmega32, the result i found is not as i expected. my expectation is that when i press the push button angle increment and bulb fade a bit till total dim. but the circuit did the first 3-4 push and unexpected result happen.
#include<avr/io.h>
#include <avr/interrupt.h>
#include <avr/eeprom.h>
#define F_CPU 8000000l
#include <util/delay.h>
uint8_t EEMEM first;
uint16_t EEMEM dummy;
unsigned int cnt=0;
int counter=0;
unsigned short ny=0;
int phaseangle=1;//phaseangle up to 100 is ok with 500us but it seems that the upper limit should be 20
int main()
{
DDRD=1<<PD2;
PORTD=1<<PD2;
DDRC=0XFF;
DDRB=0X00;//SETTING THE PORT FOR INPU
PORTB=0XFF;//ENABLING PULL UPS
GICR=1<<INT0;
MCUCR=1<<1 | 1<<0;
sei();
while(1)
{
if( (PINB & (1<<PB0) )==0)
{
cli();
_delay_ms(10);//wait till debounce fade out
phaseangle=phaseangle+1;
sei();
};
if( (PINB & (1<<PB5) )==0)
{
cli();
_delay_ms(10);//wait till debounce fade out
phaseangle=phaseangle-1;
sei();
};
}
return(1);
}
ISR(INT0_vect)
{
PORTC=0;
//i have to write something that let the process to delay before sending pulse to
//the scr gate
// our frequency is 50Hz therefore one positive or negative cycle will have 1/(2*50) seconds
//duration (1/100)*1000ms=10ms
//or (1/100)*1000000us=10000us;
for (ny=0;ny<phaseangle;ny++)
_delay_us(5); //try with higher number like 7000
//now send pulse to the gate here
PORTC=1;
//PORTC=1;
//PORTC=1;
}
#include<avr/io.h>
#include <avr/interrupt.h>
#include <avr/eeprom.h>
#define F_CPU 8000000l
#include <util/delay.h>
uint8_t EEMEM first;
uint16_t EEMEM dummy;
unsigned int cnt=0;
int counter=0;
unsigned short ny=0;
int phaseangle=1;//phaseangle up to 100 is ok with 500us but it seems that the upper limit should be 20
int main()
{
DDRD=1<<PD2;
PORTD=1<<PD2;
DDRC=0XFF;
DDRB=0X00;//SETTING THE PORT FOR INPU
PORTB=0XFF;//ENABLING PULL UPS
GICR=1<<INT0;
MCUCR=1<<1 | 1<<0;
sei();
while(1)
{
if( (PINB & (1<<PB0) )==0)
{
cli();
_delay_ms(10);//wait till debounce fade out
phaseangle=phaseangle+1;
sei();
};
if( (PINB & (1<<PB5) )==0)
{
cli();
_delay_ms(10);//wait till debounce fade out
phaseangle=phaseangle-1;
sei();
};
}
return(1);
}
ISR(INT0_vect)
{
PORTC=0;
//i have to write something that let the process to delay before sending pulse to
//the scr gate
// our frequency is 50Hz therefore one positive or negative cycle will have 1/(2*50) seconds
//duration (1/100)*1000ms=10ms
//or (1/100)*1000000us=10000us;
for (ny=0;ny<phaseangle;ny++)
_delay_us(5); //try with higher number like 7000
//now send pulse to the gate here
PORTC=1;
//PORTC=1;
//PORTC=1;
}
Attachments
-
103.7 KB Views: 16