120 lines
2.9 KiB
C
Executable File
120 lines
2.9 KiB
C
Executable File
/*================================================================================================
|
|
|
|
CMtec BOSCH_SpeedControlUnit copyright 2008 v1.0 (2008-08-07)
|
|
|
|
Name: Christoffer Martinsson
|
|
E-mail: cm@cmtec.se
|
|
|
|
Uses:
|
|
PB1 = PWM output
|
|
PB2 = Sensor input
|
|
|
|
================================================================================================*/
|
|
|
|
#include <avr/delay.h>
|
|
#include <string.h>
|
|
#include <inttypes.h>
|
|
#include <avr/io.h>
|
|
#include <avr/interrupt.h>
|
|
#include <avr/signal.h>
|
|
#include <avr/eeprom.h>
|
|
|
|
|
|
|
|
/*================================================================================================
|
|
Functions
|
|
================================================================================================*/
|
|
/*------------------------------------------------------------------------------------------------
|
|
initIO
|
|
|
|
Description: Initialise I/O
|
|
Input: -
|
|
Output: -
|
|
------------------------------------------------------------------------------------------------*/
|
|
void initIO (void){
|
|
|
|
/* Start PLL */
|
|
PLLCSR = (1<<PLLE);
|
|
loop_until_bit_is_set(PLLCSR, PLOCK);
|
|
PLLCSR |= (1<<PCKE);
|
|
|
|
/* Direction settings for port B */
|
|
DDRB = (1<<PB1);
|
|
|
|
/* Timer/PWM settings */
|
|
OCR1A = 0; // PWM value = 0
|
|
OCR1C = 255; // ~62kHz
|
|
TCCR1 = (1<<PWM1A)|(1<<COM1A0)|(1<<COM1A1)|(1<<CS11)|(1<<CS10);
|
|
|
|
/* ADC settings */
|
|
DIDR0 = (1<<ADC1D);
|
|
ADMUX = (1<<ADLAR)|(1<<MUX0);
|
|
ADCSRA = (1<<ADEN);
|
|
|
|
}
|
|
/*------------------------------------------------------------------------------------------------
|
|
readSensor
|
|
|
|
Description: Reading value from distance-sensor
|
|
Input: -
|
|
Output: -
|
|
------------------------------------------------------------------------------------------------*/
|
|
unsigned char readSensor (void){
|
|
|
|
ADCSRA |= (1<<ADSC);
|
|
loop_until_bit_is_clear(ADCSRA,ADSC);
|
|
return ADCH;
|
|
|
|
}
|
|
|
|
/*------------------------------------------------------------------------------------------------
|
|
setPWM
|
|
|
|
Description: Set PMW output
|
|
Input: -
|
|
Output: -
|
|
------------------------------------------------------------------------------------------------*/
|
|
void setPWM (unsigned char pwmValue){
|
|
|
|
if(pwmValue < 255 && pwmValue > 70)
|
|
{
|
|
pwmValue = pwmValue - 70;
|
|
OCR1A = pwmValue;
|
|
}
|
|
else if(pwmValue <= 70)
|
|
{
|
|
OCR1A = 0;
|
|
}
|
|
else
|
|
{
|
|
OCR1A = pwmValue;
|
|
}
|
|
|
|
}
|
|
|
|
/*================================================================================================
|
|
Main
|
|
================================================================================================*/
|
|
int main (void){
|
|
|
|
unsigned char tmp = 0;
|
|
|
|
cli(); // Disable global interrupt
|
|
|
|
initIO();
|
|
|
|
sei(); // Enable global interrupt
|
|
|
|
|
|
while(1){
|
|
|
|
setPWM(readSensor());
|
|
}
|
|
|
|
|
|
return (0);
|
|
}
|
|
/*================================================================================================
|
|
End
|
|
================================================================================================*/
|
|
|