movlw 0x20 #endif call scan movlw 0xfd tris PORTB ; select colb (RB1) #ifdef MODE_CH8 movlw 0x04 #endif #ifdef MODE_CH4 movlw 0x30 #endif call scan movf cod, W bz loop2 ; if no buton is pressed, skip subwf prevcod, W ; if the same button is pressed, skip bz loop2 movf cod, W movwf prevcod ; a new button is pressed - rcnt=3 movwf (mtx_buffer+1) movlw 3 movwf rcnt movlw 0x40 ; new button - new transmission addwf tcnt, F loop2 movlw 0xf7 1
tris PORTB ; select ID (RB3) call scanid movf cod0, W movwf (mtx_buffer) loop3 movf (mtx_buffer+1), W andlw 0x3f iorwf tcnt, W movwf (mtx_buffer+1) call mtx_send movf rcnt, W bz loop_done decfsz rcnt, F goto loop loop_done movf cod, W btfsc STATUS, Z goto loop0 ; no button was pressed, go sleep ; if the same button is being hold, repeat the transmission goto loop scan movwf cod0 ;movlw 0xc0 scandelay ;addlw 1 ;bnz scandelay movlw 0xf0 2
andwf PORTB, W movwf rowstate incf cod0, F btfss rowstate, 4 goto pressed incf cod0, F btfss rowstate, 5 goto pressed incf cod0, F btfss rowstate, 6 goto pressed incf cod0, F btfss rowstate, 7 goto pressed retlw 0 pressed movf cod0, W movwf cod return scanid clrf cod0 clrw scandelay2 addlw 1 bnz scandelay2 3
movlw 0xf0 andwf PORTB, W movwf rowstate btfss rowstate, 7 bsf cod0, 3 btfss rowstate, 6 bsf cod0, 2 btfss rowstate, 5 bsf cod0, 1 btfss rowstate, 4 bsf cod0, 0 return end RF RECEIVER Filename: decoder.asm NOTES PA0-PA3-> outputs PA4 -> valid reception PA5 <- RX input PB0-PB3<- address bits PB4-PB7-> outputs 4
list p=16f628 CONFIG _CP_OFF & _WDT_OFF & _PWRTE_ON & _HS_OSC & _LVP_OFF & _MCLRE_OFF & _BODEN_ON #include <p16f628.inc> #include "mrxv4.inc" EXPIRE_TIMER EQU 0x12 LATCH_MASK EQU B'11110000' VALID_BIT EQU 4.mainda UDATA 0x20 savew1 res 1 savestatus res 1 savepclath res 1 savefsr res 1 bt res 1 expire_cnt res 1 cur_seq res 1 cur_ch res 1 cur_state res 1 vectors CODE 0 goto main ; go to beginning of program nop nop nop 5
goto itr ; interrupt vector location prog CODE 5 channel_lookup andlw 0x07 addwf PCL, F dt 0x1, 0x2, 0x4, 0x8 dt 0x10, 0x20, 0x40, 0x80 itr movwf savew1 movf STATUS,w clrf STATUS movwf savestatus movf PCLATH,w movwf savepclath clrf PCLATH movf FSR,w movwf savefsr btfsc INTCON, T0IF call t0_int_handler movf savefsr,w movwf FSR 6
movf savepclath,w movwf PCLATH movf savestatus,w movwf STATUS swapf savew1,f swapf savew1,w retfie main ;HARDWARE INIT movlw 7 movwf CMCON movlw (1<<VALID_BIT) MOVWF PORTA clrf PORTB BANKSEL TRISA movlw 0x20 ; A5=input movwf TRISA MOVLW 0x0f ; B0-B3=input movwf TRISB 7
; setup TMR0 interrupt clrwdt ; changing default presc. assignment movlw 0x03 ; prescaler 1:16 assigned to TMR0 movwf OPTION_REG ; T0CS selects internal CLK bsf INTCON, T0IE ; enable TMR0 int BANKSEL TMR0 clrf TMR0 clrf expire_cnt call mrx_init bsf INTCON, GIE warm clrf cur_seq clrf cur_state incf cur_seq, F loop call mrx_receive andlw 0xff bnz loop call mrx_chk_buf andlw 0xff bnz loop movf PORTB, W andlw 0x0f subwf (mrx_buffer), W 8
bnz loop rx_ok movlw EXPIRE_TIMER ; indicate packet reception movwf expire_cnt movf (mrx_buffer+1), W ; if (seq==cur_seq) skip (only expire timer is updated) andlw 0xc0 subwf cur_seq, W bz loop movf (mrx_buffer+1), W andlw 0xc0 movwf cur_seq movf (mrx_buffer+1), W andlw 0x3f bz loop ; illegal channel data addlw 0xff andlw 0x0f movwf bt btfsc bt, 3 goto loop ; ch>=8: illegal with this model call channel_lookup movwf cur_ch bcf INTCON, GIE btfsc (mrx_buffer+1), 5 9
goto state_on_off ; 00-0f: toggle or momentary ON movf cur_ch, W andlw LATCH_MASK bz state_on_off ; momentary ON state_toggle movf cur_ch, W xorwf cur_state, F goto state_done state_on_off movf cur_ch, W xorlw 0xff andwf cur_state, F ; 20-2f: latch ON movf cur_ch, W btfss (mrx_buffer+1), 4 ; 30-3f: latch OFF iorwf cur_state, F goto state_done state_done bsf STATUS, C ; set valid led ON call state_out bsf INTCON, GIE goto loop t0_int_handler bcf INTCON, T0IF movf expire_cnt, F bnz valid_on 10
movlw LATCH_MASK ; clear momentary outputs andwf cur_state, F bcf STATUS, C ; set valid led OFF state_out ; C: turn valid bit on or off? movlw ~(1<<VALID_BIT) andwf cur_state, W ; set valid led ON btfss STATUS, C iorlw (1<<VALID_BIT) ; set valid led OFF movwf PORTA movf cur_state, W movwf PORTB return valid_on decf expire_cnt, F ;;bsf RX_LED return end PROGAM FOR PIC16F690 SERVO MOTOR CONTROL #include <htc.h> /* PIC Configuration Bit: ** INTIO - Using Internal RC No Clock ** WDTDIS - Wacthdog Timer Disable ** PWRTEN - Power Up Timer Enable 11
** MCLREN - Master Clear Enable ** UNPROTECT - Code Un-Protect ** UNPROTECT - Data EEPROM Read Un-Protect ** BORDIS - Borwn Out Detect Disable ** IESODIS - Internal External Switch Over Mode Disable ** FCMDIS - Monitor Clock Fail Safe Disable */ CONFIG(FOSC_INTRCIO & WDTE_OFF & PWRTE_ON & MCLRE_ON & CP_OFF & CPD_OFF & BOREN_OFF & IESO_OFF & FCMEN_OFF); // Using Internal Clock of 8 Mhz #define FOSC 8000000L // Servo definition and variables #define MAX_VALUE 200 #define CCW_ROTATION MAX_VALUE - 20 #define CW_ROTATION MAX_VALUE - 10 #define STOP_ROTATION MAX_VALUE #define THRESHOLD_VALUE 50 unsigned char pulse_max=0; unsigned char pulse_top=0; unsigned char top_value = 0; static void interrupt isr(void) { if(t0if) { // TIMER0 Interrupt Flag pulse_max++; // Pulse Max Increment pulse_top++; // Pulse Top Increment /* MAX_VALUE=200 turn off the pulse */ 12
if (pulse_max >= MAX_VALUE) { pulse_max=0; pulse_top=0; RC2=0; // Turn off RC2 } /* top_value = MAX_VALUE - n, n=10: 10 x 0.1ms = 1.0ms, n=20: 20 x 0.1ms = 2.0ms */ /* 2ms -> CCW Rotation, 1ms -> CW Rotation */ if (pulse_top == top_value) { RC2=1; // Turn On RC2 } TMR0 = 156; // Initial Value for 0.1ms Interrupt T0IF = 0; // Clear TIMER0 interrupt flag } } void main(void) { unsigned char ldr_left; unsigned char ldr_right; int ldr_diff; OSCCON=0x70; // Select 8 Mhz internal clock /* Initial Port Used */ TRISC = 0x03; // Set RC0 and RC1 as input others as Output ANSEL = 0x30; // Set PORT AN4 and AN5 as analog input ANSELH = 0x00; // Set PORT AN8 to AN11 as Digital I/O PORTC = 0x00; // Turn Off all PORTC /* Init Servo Pulse */ 13
pulse_max=0; pulse_top=0; top_value = MAX_VALUE; // top_value = MAX_VALUE: Servo Motor Stop /* Initial ADC */ ADCON1=0b00110000; // Select the FRC for 8 Mhz /* Init TIMER0: Period: Fosc/4 x Prescaler x TMR0 0.0005 ms x 2 * 100 = 0.1 ms */ OPTION_REG = 0b00000000; // 1:2 Prescaller TMR0=156; // Interupt every 0.1 ms T0IE = 1; // Enable interrupt on TMR0 overflow GIE = 1; // Global interrupt enable for(;;) { /* Read the ADC here */ ADCON0=0b00010001; // select left justify result. ADC port channel AN4 GO_DONE=1; // initiate conversion on the channel 4 while(go_done) continue; // Wait for ldr_left conversion done ldr_left=adresh; // Read 8 bits MSB, Ignore 2 bits LSB in ADRESL ADCON0=0b00010101; // select left justify result. ADC port channel AN5 GO_DONE=1; // initiate conversion on the channel 5 while(go_done) continue; // Wait for ldr_right conversion done ldr_right=adresh; // Read 8 bits MSB, Ignore 2 bits LSB in ADRESL 14
/* Get the different */ ldr_diff=ldr_left - ldr_right; if ((ldr_diff >= -THRESHOLD_VALUE) && (ldr_diff <= THRESHOLD_VALUE)) { top_value = MAX_VALUE; // Stop the Servo Motor } else { if (ldr_diff > THRESHOLD_VALUE) { top_value = CCW_ROTATION; // Counterclockwise Rotation } else { top_value = CW_ROTATION; // Clockwise Rotation } } } } /* EOF: servo.c */ 15
PCB LAYOUT RF TRANSMITTER RF RECEIVER: 16
Created by Simpo PDF Creator Pro (unregistered version) http://www.simpopdf.com MAIN PROJECT 2012 ESC DRIVER: RUDDER: 17
Created by Simpo PDF Creator Pro (unregistered version) http://www.simpopdf.com MAIN PROJECT 2012 FLAPRON RIGHT AND ELEVATOR: FLAPRON LEFT: 18
DATA SHEETS 19
20
21
22
23
If you are really interested in this projetct,then for any help pls feel free to contact : pradu333@gmail.com 24