Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#include "driver_config.h"
#include "target_config.h"
#include "uart.h"
#include "gpio.h"
#include "Timer16.h"
extern volatile uint32_t UARTCount;
extern volatile uint8_t UARTBuffer[BUFSIZE];
char controller = 'a';
int brightnessCount = 0;
int brightness1 = 10000;
int brightness2 = 9500;
int main (void) {
UARTInit(9600);
#if MODEM_TEST
ModemInit();
#endif
//Pin P1.9 & P1.10 - These are the pins that are going to be used
//Variables to control the brightness of the light (how far the servo goes)
int off1 = brightness1 * 0;
int veryDim1 = brightness1 * 0.25;
int dim1 = brightness1 * 0.50;
int bright1 = brightness1 * 0.75;
int veryBright1 = brightness1 * 1;
int off2 = brightness2 * 0;
int veryDim2 = brightness2 * 0.25;
int dim2 = brightness2 * 0.50;
int bright2 = brightness2 * 0.75;
int veryBright2 = brightness2 * 1;
//Initialization
volatile uint32_t period = 48000;
GPIOInit();
SystemCoreClockUpdate();
init_timer16( 0, SystemCoreClock/1000-1 );
enable_timer16(0);
init_timer16PWM(1, period, MATCH0, 0);
init_timer16PWM(1, period, MATCH1, 0);
setMatch_timer16PWM( 1, 0, period/4 );
setMatch_timer16PWM( 1, 1, period/4 );
int i = 0;
double frequency = 0.1; //0.1 for ClockWise and 0.9 for CounterClockWise
//The main infinite loop
while(1)
{
//Putting the value received into the 'controller' variable
controller = *UARTBuffer;
LPC_UART->IER = IER_THRE | IER_RLS | IER_RBR; /* Re-enable RBR */
//if( UARTCount != 0 )
{
UARTCount = 0;
//This code is to move the servo into the 'off' position
if( controller == '2' )
{
//Checks first whether the position is less than the desired position or not
if( brightnessCount < off1 )
{
//CounterClockWise
while( brightnessCount != off2 ) {
brightnessCount++;
frequency = 0.9;
init_timer16PWM( 1, period, MATCH1, 0 );
enable_timer16(1);
setMatch_timer16PWM( 1, 1, period * (1 - frequency) );
disable_timer16(1);
}
for( i = 0; i < 50000; i++ ); //Delay
}
//Checks first whether the position is greater than the desired position or not
else if( brightnessCount > off2 )
{
//ClockWise
while( brightnessCount != off1 ) {
brightnessCount--;
frequency = 0.1;
init_timer16PWM( 1, period, MATCH0, 0 );
enable_timer16(1);
setMatch_timer16PWM( 1, 0, period * (1 - frequency) );
disable_timer16(1);
}
for( i = 0; i < 50000; i++ ); //Delay
}
}
//This code is to move the servo into the 'very dim' position
else if( controller == '3' )
{
//Checks first whether the position is less than the desired position or not
if( brightnessCount < veryDim1 )
{
//CounterClockWise
while( brightnessCount != veryDim2 ) {
brightnessCount++;
frequency = 0.9;
init_timer16PWM( 1, period, MATCH1, 0 );
enable_timer16(1);
setMatch_timer16PWM( 1, 1, period * (1 - frequency) );
disable_timer16(1);
}
for( i = 0; i < 50000; i++ ); //Delay
}
//Checks first whether the position is greater than the desired position or not
else if( brightnessCount > veryDim2 )
{
//ClockWise
while( brightnessCount != veryDim1 ) {
brightnessCount--;
frequency = 0.1;
init_timer16PWM( 1, period, MATCH0, 0 );
enable_timer16(1);
setMatch_timer16PWM( 1, 0, period * (1 - frequency) );
disable_timer16(1);
}
for( i = 0; i < 50000; i++ ); //Delay
}
}
//This code is to move the servo into the 'dim' position
else if( controller == '4' )
{
//Checks first whether the position is less than the desired position or not
if( brightnessCount < dim1 )
{
//CounterClockWise
while( brightnessCount != dim2 ) {
brightnessCount++;
frequency = 0.9;
init_timer16PWM( 1, period, MATCH1, 0 );
enable_timer16(1);
setMatch_timer16PWM( 1, 1, period * (1 - frequency) );
disable_timer16(1);
}
for( i = 0; i < 50000; i++ ); //Delay
}
//Checks first whether the position is greater than the desired position or not
else if( brightnessCount > dim2 )
{
//ClockWise
while( brightnessCount != dim1 ) {
brightnessCount--;
frequency = 0.1;
init_timer16PWM( 1, period, MATCH0, 0 );
enable_timer16(1);
setMatch_timer16PWM( 1, 0, period * (1 - frequency) );
disable_timer16(1);
}
for( i = 0; i < 50000; i++ ); //Delay
}
}
//This code is to move the servo into the 'bright' position
else if( controller == '5' )
{
//Checks first whether the position is less than the desired position or not
if( brightnessCount < bright1 )
{
//CounterClockWise
while( brightnessCount != bright2 ) {
brightnessCount++;
frequency = 0.9;
init_timer16PWM( 1, period, MATCH1, 0 );
enable_timer16(1);
setMatch_timer16PWM( 1, 1, period * (1 - frequency) );
disable_timer16(1);
}
for( i = 0; i < 50000; i++ ); //Delay
}
//Checks first whether the position is greater than the desired position or not
else if( brightnessCount > bright2 )
{
//ClockWise
while( brightnessCount != bright1 ) {
brightnessCount--;
frequency = 0.1;
init_timer16PWM( 1, period, MATCH0, 0 );
enable_timer16(1);
setMatch_timer16PWM( 1, 0, period * (1 - frequency) );
disable_timer16(1);
}
for( i = 0; i < 50000; i++ ); //Delay
}
}
//This code is to move the servo into the 'very bright' position
else if( controller == '6' )
{
//Checks first whether the position is less than the desired position or not
if( brightnessCount < veryBright1 )
{
//CounterClockWise
while( brightnessCount != veryBright2 ) {
brightnessCount++;
frequency = 0.9;
init_timer16PWM( 1, period, MATCH1, 0 );
enable_timer16(1);
setMatch_timer16PWM( 1, 1, period * (1 - frequency) );
disable_timer16(1);
}
for( i = 0; i < 50000; i++ ); //Delay
}
//Checks first whether the position is greater than the desired position or not
else if( brightnessCount > veryBright2 )
{
//ClockWise
while( brightnessCount != veryBright1 ) {
brightnessCount--;
frequency = 0.1;
init_timer16PWM( 1, period, MATCH0, 0 );
enable_timer16(1);
setMatch_timer16PWM( 1, 0, period * (1 - frequency) );
disable_timer16(1);
}
for( i = 0; i < 50000; i++ ); //Delay
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.