How to write a C Program to Police Bicycle Lamp Hacker in C Programming Language ?
Solution For C Program :
/*
* Policyjna Lampa Rowerowa Hackera
*/
#define F_CPU 1000000UL // Define the speed the clock is running at.
// Used for the delay.h functions
#include <avr/io.h>
#include <avr/sleep.h>
#include <util/delay.h>
// Outputs
#define blueLED PB0
#define redLED PB1
#define whiteLED PB2
#define speaker PB4
// Inputs
#define reedSwitch PB3
// Config
#define lowFreq 145 // 145 - 440Hz
#define highFreq 45 // 45 - 1400Hz
#define stepsUp 100
#define stepsDown 200
#define multiUp ( lowFreq - highFreq ) / stepsUp
#define multiDown ( lowFreq - highFreq ) / stepsDown
#define sleep 10
char blinkCounter = 0;
int stopCounter = 0;
char reset_cntr __attribute__ ((section (".noinit")));
char mode __attribute__ ((section (".noinit"))); // biała lampa / policja
// Prototypes
void Delay_ms ( int cnt );
void init_io ( void );
void ResetHandler ( void );
void PoliceLamp ( void );
void WhiteLamp ( void );
void Sound ( char );
void Blink ( void );
void PowerOff ( void );
// Functions
void Delay_ms( int cnt )
{
while( cnt-- > 0 ) _delay_ms( 1 );
}
void init_io( void )
{
if ( mode )
{ // Police Lamp
DDRB = ( 1 << redLED ) | ( 1 << blueLED ) | ( 1 << speaker ); // Set pins as outputs
// TCCR1: Timer/Counter1 Control Register
// CTC1 PWM1A COM1A1 COM1A0 CS13 CS12 CS11 CS10
// Prescale Select
TCCR1 = ( 0 << CS13 ) | ( 1 << CS12 ) | ( 0 << CS11 ) | ( 1 << CS10 );
//General Timer/Counter Control Register
GTCCR = ( 1 << PWM1B ) | ( 1 << COM0B1 ) | ( 0 << COM0B0 );
// OCR1A = 100; // wtf
OCR1B = 5; // szerokość impulsu
OCR1C = 0; // czestotliwosc
}
else
{ // White Lamp
DDRB = ( 1 << whiteLED ); // Set pins as outputs
}
PORTB = ( 1 << reedSwitch ); // włącza PullUp na WEJŚCIU
}
void ResetHandler( void )
{
// MCUSR: 0 0 0 0 WDRF BORF EXTRF PORF
if ( MCUSR & 0x1 ) // PORF
{
reset_cntr = 0;
mode = 0;
MCUSR = 0;
}
if ( MCUSR & 0x2 ) // EXTRF
{
reset_cntr++;
MCUSR = 0;
}
/* // BORF nie działa w moim attiny :/
if (MCUSR & 0x3 ) // BORF
{
MCUSR = 0;
PowerOff();
}
*/
if ( reset_cntr >= 2 )
{
mode = ~mode;
reset_cntr = 0;
}
}
void Sound ( char i )
{
OCR1C = i;
Delay_ms( sleep );
}
void Blink( void )
{
blinkCounter++;
if ( blinkCounter % 6 == 0 ) PORTB &= ~( ( 1 << redLED ) |
( 1 << blueLED ) );
if ( blinkCounter % 6 == 3 )
{
if ( blinkCounter < 60 ) PORTB |= ( 1 << redLED );
else PORTB |= ( 1 << blueLED );
}
if ( blinkCounter > 120 ) blinkCounter = 0;
}
void PoliceLamp ( void )
{
while ( stopCounter++ < 10 ) // was 2
{
for (int i = stepsUp; i >= 1; i-- )
{
Sound( i * multiUp + highFreq );
Blink();
if ( ( PINB & ( 1 << reedSwitch ) ) == 0 ) stopCounter = 0;
}
for (int i = 1; i <= stepsDown; i++ )
{
Sound( i * multiDown + highFreq );
Blink();
if ( ( PINB & ( 1 << reedSwitch ) ) == 0 ) stopCounter = 0;
}
reset_cntr = 0;
}
Sound( 0 );
}
void WhiteLamp( void )
{
while ( stopCounter++ < 10 ) // was 2
{
for ( int i = 1; i < 300; i++ )
{
PORTB |= ( 1 << whiteLED );
Delay_ms( 5 );
//PORTB &= ~( 1 << whiteLED );
Delay_ms( 3 );
if ( ( PINB & ( 1 << reedSwitch ) ) == 0 ) stopCounter = 0;
}
reset_cntr = 0;
}
}
void PowerOff ( void )
{
PORTB = 0;
DDRB = 0; // ważne dla oszczędzenia prądu!
set_sleep_mode( SLEEP_MODE_PWR_DOWN );
sleep_mode( );
}
//Main Function
int main( void )
{
ResetHandler( );
init_io( );
if ( mode ) PoliceLamp( );
else WhiteLamp ( );
PowerOff( );
}