📄 pran.c
字号:
//Pran.c : source file for the Kapalbharti Prnayam project
#include <Interrupt.h>
#include <delay.h>
int count = 0; //for actual timer count on seven segment display
int timer_count=0; //used to calculate the time in seconds. for sevend segment display
int buzzer = 0; // 0 - buzzer off , 1 - buzzer on
int buzzer_setting = 0; //0 - timer0, 1 - timer1, 2 - timer2
int ontime = 0;
int offtime = 3,offtimetemp = 3; // = 4 for timer1 (1.5 sec), and = 5 for timer2(2sec). These are calcuilated.
int setting = 1; // this setting makes the count of 1 second in case when off time of timer is 1sec..
void decode_count(int);
void decode_buzzer_count(int);
static void io_init(void);
void extint_init(void);
void timers_init(void);
uint8_t send_data_to_decoder_high(int);
uint8_t send_data_to_decoder_low(int);
void temp(int);
static void io_init(void)
{
// PortB //output port
PORTB = 0xff;
DDRB = 0xff;
// PortC //output port
PORTC = 0xff;
DDRC = 0xff;
// PortA //input port
PORTA = 0xff;
DDRA = 0x0;
// PortD //output port(PORTD2) and input port as interrupt is to be recieved
PORTD = 0x0;
DDRD = 0xfb;
}
void extint_init(void)
{
// INT0 Enabled, Mode: Falling Edge
MCUCR = 0x02;
GICR = 0x40;
}
ISR(SIG_INTERRUPT0)
{
int led;
/*
All input switches will connected to their corresponding port lines, and also all
switches will be connnected to PORTD2 (pin no. 16).
*/
// INT0 Enabled, Mode: Falling Edge
led = PINA;
MCUCR = 0x02;
GICR = 0x40;
if(led == 254) //if switch to 'increase the count' is pressed, PORTA0 (pin no. 40)
{
if(count != 9) {
count++;
decode_count(count);
}
sei();
PORTA = 0xff;
}
if(led == 253) //if switch to 'decrease the count' is pressed, PORTA1 (pin no. 39)
{
if(count != 0) {
count--;
decode_count(count);
}
sei();
PORTA = 0xff;
}
if(led == 251) //if switch to 'start the timer' is pressed, PORTA2 (pin no. 38)
{
if(count != 0)
{
timers_init();
PORTA = 0x00; //As the 'start timer' button is pressed, all keys will be deactivated.
PORTA = 0xff;
PORTD = 0x0;
DDRD = 0xff;
}
}
if(led == 247) //if switch to 'increase buzzer delay' is pressed, PORTA3 (pin no. 37)
{
if(buzzer_setting != 2) {
buzzer_setting++;
temp(buzzer_setting);
}
sei();
PORTA = 0xff;
}
if(led == 239) //if switch to 'decrease buzzer delay' is pressed, PORTA4 (pin no. 36)
{
if(buzzer_setting != 0) {
buzzer_setting--;
temp(buzzer_setting);
}
sei();
PORTA = 0xff;
}
}
void temp(int te) {
if(te == 0) {
PORTB = 0x01;
offtime = 3;
offtimetemp = 3;
}
else if(te == 1) {
PORTB = 0x02;
offtime = 4;
offtimetemp = 4;
}
else if(te == 2) {
PORTB = 0x04;
offtime = 5;
offtimetemp = 5;
}
}
void timers_init(void)
{
// Timer/Counter1 Clock source: T1 Falling Edge
// Timer/Counter1 Mode: Normal
// Timer/Counter1 Output: A: Disconnected, B: Disconnected //0.5 second
OCR1A = 0x1e84;
TCNT1 = 0xe17c;
TCCR1B = 0x04;
TIMSK = 0x14;
}
ISR(SIG_OUTPUT_COMPARE1A)
{
int i;
// Timer/Counter1 Clock source: T1 Falling Edge
// Timer/Counter1 Mode: Normal
// Timer/Counter1 Output: A: Disconnected, B: Disconnected
timer_count++;
if(ontime != 0) {
PORTD = 0x0; //switch off the buzzer
offtime++;
}
if(timer_count == 120) {
count--;
if(count == 0) {
decode_count(count);
PORTD = 0x10;
for(i=0 ; i<200 ; i++)
_delay_loop_2(30000);
for(;;)
PORTD = 0x0;
}
decode_count(count);
timer_count = 0;
}
OCR1A = 0x1e84;
TCNT1 = 0xe17c;
TCCR1B = 0x04;
TIMSK = 0x14;
}
ISR(SIG_OVERFLOW1)
{
if(offtime % offtimetemp == 0) {
PORTD = 0x10; //switch on the buzzer
ontime++;
}
}
uint8_t send_data_to_decoder_high(int digit)
{
uint8_t pass = 0x00;
switch(digit)
{
case 0: pass = 0x00;
break;
case 1: pass = 0x10;
break;
case 2: pass = 0x20;
break;
case 3: pass = 0x30;
break;
case 4: pass = 0x40;
break;
case 5: pass = 0x50;
break;
case 6: pass = 0x60;
break;
case 7: pass = 0x70;
break;
case 8: pass = 0x80;
break;
case 9: pass = 0x90;
break;
}
return pass;
}
uint8_t send_data_to_decoder_low(int digit)
{
uint8_t pass = 0x00;
switch(digit)
{
case 0: pass = 0x00;
break;
case 1: pass = 0x01;
break;
case 2: pass = 0x02;
break;
case 3: pass = 0x03;
break;
case 4: pass = 0x04;
break;
case 5: pass = 0x05;
break;
case 6: pass = 0x06;
break;
case 7: pass = 0x07;
break;
case 8: pass = 0x08;
break;
case 9: pass = 0x09;
break;
}
return pass;
}
void decode_count(int county)
{
PORTC = send_data_to_decoder_high(county);
}
void decode_buzzer_count(int county)
{
int digit1,digit2;
digit2 = county/10;
PORTB = send_data_to_decoder_low(digit2);
digit1 = (county - digit2 * 10);
PORTB |= send_data_to_decoder_high(digit1);
}
int main(void)
{
io_init();
extint_init();
sei();
decode_buzzer_count(10);
decode_count(0);
while(1){}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -