Les 6 - onderwerpen
description
Transcript of Les 6 - onderwerpen
2PROJ5 – PIC assembler
Hogeschool Utrecht / Institute for Computer, Communication and Media Technology
1
Les 6 - onderwerpen
• Volgende weken
• Keypad, dender (bouncing)
• U(S)ART
• IR
• Motortjes
• C library
• Opdrachten:
– toetsenbord en UART (asm),
– LCD (C)
2PROJ5 – PIC assembler
Hogeschool Utrecht / Institute for Computer, Communication and Media Technology
2
Na vandaagWeek 22
maandag 32 mei : inhaalles voor de maandag-klas
Week 24 : projectweek!
maandag 14 juni : SLO / ABC
docent(en) aanwezig : wo, vry: 10:00 – 14:00
Week 25 : projectweek!
docent(en) aanwezig : di: 10:00 – 14:00
donderdag 24 juni : beoordeling woensdag-klas 10:00 – 15:00
vrijdag 25 juni : beoordeling maandag-klas 10:00 – 15:00
Practicum opgaven kunnen uiterlijk dinsdag 26 juni worden afgetekend.
2PROJ5 – PIC assembler
Hogeschool Utrecht / Institute for Computer, Communication and Media Technology
3
keypad
DB038 manual 2.18
2PROJ5 – PIC assembler
Hogeschool Utrecht / Institute for Computer, Communication and Media Technology
4
Keypad lezen
• Maak pin RA1 een ingang (bit 1 in TRISA)
• Maak poort A een digitale poort:
ANSEL = 0
• Zet een waarde 0x01, 0x02, 0x04 of 0x08 op PORTD
• Zet een waarde 4 .. 7 op PORTE
• Lees pin RA1
• Doe dit voor alle 16 toetsen
DB038 manual 2.18
2PROJ5 – PIC assembler
Hogeschool Utrecht / Institute for Computer, Communication and Media Technology
5
Keypad lezen - demoerrorlevel -307
errorlevel -302
#include <DB038-01.INC>
#include <kbd.inc>
main_loop:
BCF PORTA_SHADOW, 2
LCALL PORTA_FLUSH
LCALL kbd_read
XORLW 0xFF
MOVWF PORTD_SHADOW
LCALL PORTD_FLUSH
MOVLW .4
MOVWF PORTE_SHADOW
LCALL PORTE_FLUSH
BSF PORTA_SHADOW, 2
LCALL PORTA_FLUSH
LCALL M10WAIT
LGOTO main_loop
END
Archive: kbd-asm.zip
2PROJ5 – PIC assembler
Hogeschool Utrecht / Institute for Computer, Communication and Media Technology
6
Keypad lezen – kbd.incLGOTO kbd_inc_end
#define kbd_0 .0
#define kbd_1 .1
#define kbd_2 .2
#define kbd_3 .3
#define kbd_4 .4
#define kbd_5 .5
#define kbd_6 .6
#define kbd_7 .7
#define kbd_8 .8
#define kbd_9 .9
#define kbd_A .10
#define kbd_B .11
#define kbd_C .12
#define kbd_D .13
#define kbd_S .14
#define kbd_H .15
#define kbd_none .16
kbd_test macro D, E, K
movlw D
movwf PORTD_SHADOW
LCALL PORTD_FLUSH
movlw E
movwf PORTE_SHADOW
LCALL PORTE_FLUSH
BTFSC PORTA, 1
RETLW K
endm
kbd_read: banksel TRISA
bsf TRISA, 1
banksel TRISD
clrf TRISD
banksel TRISE
clrf TRISE
banksel ANSEL
clrf ANSEL
banksel 0
kbd_test 1, 4, kbd_S
kbd_test 1, 5, kbd_0
kbd_test 1, 6, kbd_H
kbd_test 1, 7, kbd_D
kbd_test 2, 4, kbd_7
kbd_test 2, 5, kbd_8
kbd_test 2, 6, kbd_9
kbd_test 2, 7, kbd_C
kbd_test 4, 4, kbd_4
kbd_test 4, 5, kbd_5
kbd_test 4, 6, kbd_6
kbd_test 4, 7, kbd_B
kbd_test 8, 4, kbd_1
kbd_test 8, 5, kbd_2
kbd_test 8, 6, kbd_3
kbd_test 8, 7, kbd_A
retlw kbd_none
kbd_inc_end:
2PROJ5 – PIC assembler
Switch bounce (denderen)
Als je een drukknop indrukt maakt hij niet 1 keer contact: hij dendert een tijdje (maximaal ~ 50 ms)
2PROJ5 – PIC assembler
PIC - USARTUSART = Universal Synchronous / Asynchronous Receiver +
Transmitter
• wij gebruiken asynchroon• PIC UART pins zijn verbonden met een FT232R USB-to-
asynchronous converter• Op de PC wordt een (virtuele) seriele poort aangemaakt (XP
en Vista hebben al een driver)control panel system hardware device manager ports
• Evt. driver van www.ftdichip.com gebruiken• op de PC gebruik je een terminal, bv HyperTerminal
(hypertrm, 19k2, no parity, no flow control, hu: com3 (?))• In de image staat een icoon ?
2PROJ5 – PIC assembler
PIC - USART
DB038 manual 2.4
power van de 2e USB poort
2PROJ5 – PIC assembler
PIC – USART - init
make TxD (RC6) output, RxD (RC7) input
SPBRG: 19k2 value for high speed
TXSTA: 8 bit, enable, asynch, high speed
RCSTA: enable, 8 bit, continuous, no ADDEN
2PROJ5 – PIC assembler
PIC – USART - send• wacht tot TSR bit aangeeft dat TSR empty is
• copy het te verzenden byte naar TXREG
• (wacht tot TSR bit aangeeft dat TSR empty is)
• als OERR bit gezet is:
– clear CREN
– wacht een paar instructies
– set CREN
• als PIR1 : TXIF op 0 gezet is:
– lees RCREG, dit is het ontvangen byte
(anders is er nog niets ontvangen)
PIC – USART - receive
2PROJ5 – PIC assembler
PIC – USART – demo code subroutines
UART_INIT : call to inistialise the UART for 19k2
UART_CHAR_SEND : sends the char in W
UART_CHAR_RECEIVE : checks the UART for a received char, C flag set when a char is received (char in W), C flag is cleared when no char is received
UART_CLRF_SEND : sends the CR LF sequence
Archive: uart.zip
2PROJ5 – PIC assembler
Hogeschool Utrecht / Institute for Computer, Communication and Media Technology
13
PIC – USART – demo code mainMAIN
CALL UART_INIT
MOVLW 'H'CALL UART_CHAR_SENDMOVLW 'i'CALL UART_CHAR_SENDCALL UART_CLRF_SEND
MAIN_LOOPCALL UART_CHAR_RECEIVESKPC
GOTO MAIN_LOOPMOVWF CharMOVLW '"'CALL UART_CHAR_SENDMOVFW CharCALL UART_CHAR_SENDMOVLW '"'CALL UART_CHAR_SENDCALL UART_CLRF_SENDGOTO MAIN_LOOP
2PROJ5 – PIC assembler
De IR ontvanger
DB038 manual 2.8
16F887 pins Direction Value
RA2 Digital input Low when an IR signal is received, otherwise high.
RE0 … RE2 Outputs 0b110
• RA2 must be a digital input
• RE0..RE2 outputs, value 0b110
2PROJ5 – PIC assembler
De IR ontvanger
• TFM5360 of TSOP1736• (alleen) gevoelig voor een ~ 36 kHz signaal.• Dat signaal mag niet continu zijn
(goed is bv.1 kHz: 500 us signaal, 500 us uit)
2PROJ5 – PIC assembler
De IR ontvanger
(alleen) gevoelig voor een 36 kHz signaal.
Bij 10% afwijking, dus 33-40 kHz, 60% vd. Afstand.
2PROJ5 – PIC assembler
De IR ontvanger
Aanbevolen: puls >= 400uS, periode =< 0.4
2PROJ5 – PIC assembler
De IR ontvanger
2PROJ5 – PIC assembler
Eenvoudig IR zenden / ontvangen
• 36 kHz 28 us per puls 5 * 28 instructies per fase (hoog en laag)
• 400 us / 28 us minimaal 14 pulsen, neem bv 30 pulsen
• Onmiddelijk daarna de IR ingang lezen (laag == signaal gedetecteerd)
• Pauze! (bv. 400 us)• herhaal
2PROJ5 – PIC assembler
Hogeschool Utrecht / Institute for Computer, Communication and Media Technology
20
DB038 - motortjesMotor
aansluitingen
Driver chips
Voeding, bv 12V
2PROJ5 – PIC assembler
Hogeschool Utrecht / Institute for Computer, Communication and Media Technology
21
H-brug (H-bridge)
Motor 1
Motor 2
2PROJ5 – PIC assembler
Hogeschool Utrecht / Institute for Computer, Communication and Media Technology
22
DB038 manual 2.19
2PROJ5 – PIC assembler
Hogeschool Utrecht / Institute for Computer, Communication and Media Technology
23
DB038 manual 2.19
16F887 pins Direction ValueRC0 Output Motor 1 ARC1 Output Motor 2 enableRC2 Output Motor 1 enableRC3 Output Motor 1 BRC4 Output Motor 2 ARC5 Output Motor 2 B
A B value Motor effect (if enable is high)0 0 Brake0 1 Forward1 0 Backward1 1 Brake
Wat moet je op RC0..RC5 zetten om het onderstaande te bereiken:
- Motor 1 (links) vooruit, motor 2 (rechts) vooruit
- Links vooruit, rechts op de rem (bocht naar rechts)
- Links achteruit, rechts vooruit (draai om de as naar links)
2PROJ5 – PIC assembler
Hogeschool Utrecht / Institute for Computer, Communication and Media Technology
24
- Gewoon in MPLAB
- DB038 manual hoofdstuk 5
- Gewoon dubbel-klikken op de .mcp file van een bestaand C project.
- Let op de omvang van de integer types
- Compiler handelt pages en banks af
- De 8-level stack limit bestaat nog steeds!
- PORTA, ADRESL, etc zijn al gedefinieerd
PIC programmeren in C
2PROJ5 – PIC assembler
Hogeschool Utrecht / Institute for Computer, Communication and Media Technology
25
integersTypes Size Remarks
Bit 1 bit Non-standard; see manual
(signed or unsigned) char 8 bits = 1 byte
(signed or unsigned) short int 16 bits = 2 bytes
(signed or unsigned) int 16 bits = 2 bytes
(signed or unsigned) short long int 24 bits = 3 bytes non-standard
(signed or unsigned) long int 32 bits = 4 bytes
Float 24 bits = 3 bytes
Double 24 bits = 3 bytes, or 32 bits = 4 bytes
see manual
2PROJ5 – PIC assembler
Hogeschool Utrecht / Institute for Computer, Communication and Media Technology
26
C libraries
Files Containdb038.hdb038.c
Configuration fuses, busy waiting, I/O port shadowing, nibble-to-char.
db038-adc.hdb038-adc.c
Read the A/D converter.
db038-dallas.hdb038-dallas.c
Access to Dallas one-wire peripherals.
db038-i2c.hdb038-i2c.c
I2C library.
db038-keypad.hdb038-keypad.c
4 x 4 keypad reading.
db038-lcd.hdb038-lcd.c
HD44780 character LCD initialization, clear, cursor position control, char print, string print, custom character definition.
db038-leds.hdb038-leds.c
Show values on the seven segment displays and the LEDs.
db038-string.hdb038-string.c
Character and string functions.
db038-uart.hdb038-uart.c
UART initialization, character send and receive, string send.
2PROJ5 – PIC assembler
Hogeschool Utrecht / Institute for Computer, Communication and Media Technology
27
LCD library
void lcd_init( unsigned char x, unsigned char y );void lcd_char_print( char c ); // note: ’\v’void lcd_string_print( const char *p );
2PROJ5 – PIC assembler
Hogeschool Utrecht / Institute for Computer, Communication and Media Technology
28
Simpel C voorbeeld
#include "db038-leds.h"
#include "db038-adc.h"
void main(void){
volatile unsigned int ad_result;
for(;;){
ad_result = adc_potmeter();
display_decimal_show( ad_result, 0, 0, 10 );
}
}
2PROJ5 – PIC assembler
Hogeschool Utrecht / Institute for Computer, Communication and Media Technology
29
oefening 1 : toetsenbord, UART (asm)
Schrijf – met behulp van de de programma’s - een programma dat het toetsenbord leest, en via de UART laat zien welke toets er is ingedrukt. Je programma moet ook reageren als er door de PC een character naar de PIC wordt verzonden. Bepaal zelf hoe.
2PROJ5 – PIC assembler
Hogeschool Utrecht / Institute for Computer, Communication and Media Technology
30
oefening 2 : LCD (C)
Schrijf – in C – een programma dat gebruik maakt van het LCD, en ten minste 1 andere peripheral (A/D, UART, toetsjes, IR, etc) op het bord.