⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 asuro 017, build 001, l-r tracksensor controls l-r breakled.c

📁 Asuro小车示例源码 L-R tracksensor controls L-R breakled
💻 C
📖 第 1 页 / 共 3 页
字号:
/*

  ================================================================================

	This program measures the amount of light that is detected by both the bottom-side
	track sensor diodes. These measured values are used to control the brightness of
	the corresponding brake leds.
	The left  brake led will shine brigther if the left  track-sensor receives more light.
	The right brake led will shine dimmer   if the right track-sensor receives more light.

	Timer/counter0 is running free, counting up from 0 to 255 and then restarts counting
	from 0. Each time timer/counter0 counts from 255 to 0, an interrupt will be generated.
	This will happen each 250us or with a frequency of 3906Hz.
	
	The timer/counter0 interrupt-handler function will start the AD-Converter to read one
	of both track sensors. As an AD-Conversion takes some time, the conversion is only
	started and if completed will generate an AD-Conversion-finished interrupt.
	The timer/counter0 interrupt function automatically toggles starting reading left or 
	right track sensor.
	
	The AD-Conversion-finished interrupt-handler stores the measured value from left
	or right track-sensor in different track-sensor-variables.
	
	Timer1 is continuously counting up and then counting douwn. Each time when value 0
	is reached an interrupt will occur. This interrupt function will copy the 
	track-sensor-variables into compare value A and compare value B.
	
	When counter1 reached compare value A while upcounting or while downcounting an
	compareA interrupt will occur. Same story applies to B generating a compareB interrupt.
	Inside both interrupt handlers the corresponding brake-leds are toggled on or off.
	
  Using: 
   - timer/counter0 with overflow interrupt
   - AD-Conversion with interrupt (interrupt toggles between reading left/right sensor)
   - timer/counter1 with zero/compareA/compareB interrupts
	 
  ================================================================================

	Program				: Asuro 017, Build 001, Left/right tracksensor controls left/right breakled.c
	Release date	: 2005-02-11
	Author				: Henk van Winkoop

	Build	: 001, 2005-02-11, original release

  ================================================================================

*/

//--------------------------------------------------------------------------------
//	INCLUDES
//--------------------------------------------------------------------------------
	#include "hvwdefines.h"
//--------------------------------------------------------------------------------
//	GLOBAL VARIABLES
//--------------------------------------------------------------------------------
	volatile byte gyCurLftTrkSnsVal;
	volatile byte gyCurRgtTrkSnsVal;
//================================================================================
//  TOGGLE LEFT BRAKE LED
//================================================================================
void vToggleLeftBrakeLed(void){
	//---toggle-tracking-led---//
	//if tracking led is on
	if(PORTC&BOD_LFT_H){
		//switch tracking led off
		SFRX(PORTC,BOD_LFT_L);
	}
	//if tracking led is off
	else{
		//switch tracking led on
		SFRX(PORTC,BOD_LFT_H);
	}
}
//================================================================================
//  TOGGLE RIGHT BRAKE LED
//================================================================================
void vToggleRightBrakeLed(void){
	//---toggle-tracking-led---//
	//if tracking led is on
	if(PORTC&BOD_RGT_H){
		//switch tracking led off
		SFRX(PORTC,BOD_RGT_L);
	}
	//if tracking led is off
	else{
		//switch tracking led on
		SFRX(PORTC,BOD_RGT_H);
	}
}
//================================================================================
//  INIT UNUSED IO PORTS
//================================================================================
void vInitUnusedIoPorts(void){

  //disconnect both left engine connections from VCC

  //+-------------------------------------------------------------------------------------------+
  //| Init microcontroller I/O-port D                                                           |
  //+-------------------------------------------------------------------------------------------+
  //|                                                                                           |
  //+-----------+---------+---------+---------+---------+---------+---------+---------+---------+
  //|port name  |   PD7   |   PD6   |   PD5   |   PD4   |   PD3   |   PD2   |   PD1   |   PD0   |
  //+-----------+---------+---------+---------+---------+---------+---------+---------+---------+
  //|bit number |    7    |    6    |    5    |    4    |    3    |     2   |     1   |    0    |
  //+-----------+---------+---------+---------+---------+---------+---------+---------+---------+
  //|pin number |   13    |   12    |   11    |    6    |    5    |     4   |     3   |    2    |
  //+-----------+---------+---------+---------+---------+---------+---------+---------+---------+
  //|signal name|BOD_COM  |TRK_LED  |REV_LFT  |FWD_LFT  |SWI_IAC  |SLD_RED  |IR_TXD2  |IR_RXD   |
  //+-----------+---------+---------+---------+---------+---------+---------+---------+---------+
  SFRX ( DDRD   ,                    REV_LFT_O|FWD_LFT_O                                        ); 
  //+-----------+---------+---------+---------+---------+---------+---------+---------+---------+
  SFRX ( PORTD  ,                    REV_LFT_H|FWD_LFT_H                                        ); 
  //+-----------+---------+---------+---------+---------+---------+---------+---------+---------+

  //disconnect both right engine connections from VCC

  //+-------------------------------------------------------------------------------------------+
  //| DDRB =  Data Direction Register port B                                                    |
  //+-------------------------------------------------------------------------------------------+
  //+-----------+---------+---------+---------+---------+---------+---------+---------+---------+
  //|port name  |   PB7   |   PB6   |   PB5   |   PB4   |   PB3   |   PB2   |   PB1   |   PB0   |
  //+-----------+---------+---------+---------+---------+---------+---------+---------+---------+
  //|bit number |    7    |    6    |    5    |    4    |    3    |     2   |     1   |    0    |
  //+-----------+---------+---------+---------+---------+---------+---------+---------+---------+
  //|pin number |   10    |    9    |   19    |   18    |   17    |    16   |    15   |   14    |
  //+-----------+---------+---------+---------+---------+---------+---------+---------+---------+
  //|signal name|  XTAL2  |  XTAL1  |REV_RGT  |FWD_RGT  |IR_TXD1  |SPD_RGT  |SPD_LFT  |SLD_GRN  |
  //+-----------+---------+---------+---------+---------+---------+---------+---------+---------+
  SFRX ( DDRB   ,                    REV_RGT_O|FWD_RGT_O                                        );
  //+-----------+---------+---------+---------+---------+---------+---------+---------+---------+
  SFRX ( PORTB  ,                    REV_RGT_H|FWD_RGT_H                                        );
  //+-----------+---------+---------+---------+---------+---------+---------+---------+---------+

  //set left/right engine speed ports as output and low (low = engines disabled)

  //+-------------------------------------------------------------------------------------------+
  //| DDRB =  Data Direction Register port B                                                    |
  //+-------------------------------------------------------------------------------------------+
  //+-----------+---------+---------+---------+---------+---------+---------+---------+---------+
  //|port name  |   PB7   |   PB6   |   PB5   |   PB4   |   PB3   |   PB2   |   PB1   |   PB0   |
  //+-----------+---------+---------+---------+---------+---------+---------+---------+---------+
  //|bit number |    7    |    6    |    5    |    4    |    3    |     2   |     1   |    0    |
  //+-----------+---------+---------+---------+---------+---------+---------+---------+---------+
  //|pin number |   10    |    9    |   19    |   18    |   17    |    16   |    15   |   14    |
  //+-----------+---------+---------+---------+---------+---------+---------+---------+---------+
  //|signal name|  XTAL2  |  XTAL1  |REV_RGT  |FWD_RGT  |IR_TXD1  |SPD_RGT  |SPD_LFT  |SLD_GRN  |
  //+-----------+---------+---------+---------+---------+---------+---------+---------+---------+
  SFRX ( DDRB   ,                                                  SPD_RGT_O|SPD_LFT_O          );
  //+-----------+---------+---------+---------+---------+---------+---------+---------+---------+
  SFRX ( PORTB  ,                                                  SPD_RGT_L|SPD_LFT_L          );
  //+-----------+---------+---------+---------+---------+---------+---------+---------+---------+
}
//================================================================================
//  INIT TIMER COUNTER 1 MODE 10
//================================================================================
void vInitTimerCounter1Mode10(void){

  //---init-timer-counter-register-1-for-controlling-engine-speeds---//

  //CLK-i/o lijkt 8MHz/2=4MHz te zijn!
  //In Waveform Generation Mode 1, 8-bit, TOP = 0xFF = 255
  //In prescaler mode 1, Counter clock (CLK-i/o) should be external clock	(8MHz) but seems to be external clock / 2 (4MHz)
  //Frequency = CLK-i/o / 8 / 256 = 4MHz / 8 / 256 = 2KHz

  //+-------------------------------------------------------------------------------------------+
  //| TCCR1B = Timer Counter 1 Control Register B                                               |
  //+-------------------------------------------------------------------------------------------+
  //|                                                                                           |
  //| CSnn = Clock Source                                                                       |
  //| 000 = no clock source, timer/counter stopped                  (counter max set to 0x00FF) |
  //| 001 = 8MHz/   1 = 8.000.000Hz => /256 = 31250.00Hz =     32us                             |
  //| 010 = 8MHz/   8 = 1.000.000Hz => /256 =  3906.25Hz =    256us                             |
  //| 011 = 8MHz/  64 =   125.000Hz => /256 =   488.28Hz =   2048us                             |
  //| 100 = 8MHz/ 256 =    31.250Hz => /256 =   122.07Hz =  8.192us                             |
  //| 101 = 8MHz/1024 =     7.813Hz => /256 =    30.52Hz = 32.768us                             |
  //| 110 = External clock source on T0 pin. Clock on falling edge                              |
  //| 111 = External clock source on T0 pin. Clock on rising edge                               |
  //|                                                                                           |
  //+-----------+---------+---------+---------+---------+---------+---------+---------+---------+
  //|bit number |    7    |    6    |    5    |    4    |    3    |     2   |     1   |    0    |
  //+-----------+---------+---------+---------+---------+---------+---------+---------+---------+
  //|signal name| ICNC1_X | ICES1_X |RESERVED | WGM13_X | WGM12_X | CS12_X  | CS11_X  | CS10_X  |
  //+-----------+---------+---------+---------+---------+---------+---------+---------+---------+
  SFRX ( TCCR1B ,
                  ICNC1_L                                                                       |  //no noise-canceler (no 4 clock AD conversion delay)
                            ICES1_L                                                             |  //no 'input capture' selection
                                     RESERVED                                                   |  //unused
                                                WGM13_H | WGM12_L                               |  //high part of timer1 operating mode 10 selection
                                                                    CS12_L  | CS11_H  | CS10_L  ); //timer1 runs at 3906Hz (see list above)
  //+-----------+---------+---------+---------+---------+---------+---------+---------+---------+



  //+-------------------------------------------------------------------------------------------+
  //| TCCR1A = Timer Counter 1 Control Register A                                               |
  //+-------------------------------------------------------------------------------------------+
  //|                                                                                           |
  //|                                                                                           |
  //|                                                                                           |
  //+-----------+---------+---------+---------+---------+---------+---------+---------+---------+
  //|bit number |    7    |    6    |    5    |    4    |    3    |     2   |     1   |    0    |
  //+-----------+---------+---------+---------+---------+---------+---------+---------+---------+

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -