Cov txheej txheem:

PIC MCU thiab Python Serial Communication: 5 Kauj Ruam
PIC MCU thiab Python Serial Communication: 5 Kauj Ruam

Video: PIC MCU thiab Python Serial Communication: 5 Kauj Ruam

Video: PIC MCU thiab Python Serial Communication: 5 Kauj Ruam
Video: Timer And Counter | Pic Microcontroller | Microcontroller 2024, Hlis ntuj nqeg
Anonim
PIC MCU thiab Python Serial Communication
PIC MCU thiab Python Serial Communication
PIC MCU thiab Python Serial Communication
PIC MCU thiab Python Serial Communication

Nyob zoo, hais mav! Hauv txoj haujlwm no kuv yuav sim piav qhia kuv qhov kev sim ntawm PIC MCU thiab Python kev sib txuas lus txuas. Hauv is taws nem, muaj ntau yam kev qhia thiab yeeb yaj kiab yuav ua li cas sib txuas lus nrog PIC MCU hla lub davhlau ya nyob twg uas muaj txiaj ntsig zoo. Txawm li cas los xij, hauv kuv txoj haujlwm tseem ceeb ib qho ntawm cov kev xav tau yog tsim kom muaj kev sib txuas lus ntawm PIC MCU thiab Python uas kuv tsis tuaj yeem pom cov lus qhia zoo. Yog li, cia peb pib:)

Kauj Ruam 1: Peb Xav Tau Dab Tsi?

Peb Xav Tau Dab Tsi?
Peb Xav Tau Dab Tsi?
Peb Xav Tau Dab Tsi?
Peb Xav Tau Dab Tsi?

Yog li, ua ntej tshaj plaws cia saib seb peb xav tau dab tsi. Hais txog kev kho vajtse:

  • Muaj tseeb, PIC MCU uas yog hauv kuv rooj plaub PIC16f877a (Koj tsis xav tau lub rooj tsavxwm ntawd. Qhov no yog txhawm rau ua kom yooj yim qee qhov kev sib txuas tsis zoo)
  • USB rau TTL hloov pauv kom sib txuas lus nrog PIC MCU hla USB chaw nres nkoj los ntawm kev siv TX thiab RX pins.
  • MCU programmer uas yog hauv kuv rooj plaub K150 programmer, tab sis koj tuaj yeem siv PicKit 2, 3 lossis lwm yam.
  • Thiab thaum kawg lub computer:)

Hais txog software:

  • Ib qho IDE txhawm rau sau tus nab npawb nab npawb uas yog hauv kuv li Pycharm, tab sis koj tuaj yeem siv Python IDE ib txwm thiab.
  • Ib puag ncig rau kev ua haujlwm MCU uas yog hauv kuv qhov xwm txheej MPLAB X IDE nrog CCS C compiler.

Kauj Ruam 2: Kev Sib Txuas Kho Vajtse

Kho vajtse sib txuas
Kho vajtse sib txuas

Hauv daim duab txuas nrog kho vajtse sib txuas tau muab uas yog nruab nrab ntawm PIC MCU thiab USB TTL hloov pauv raws li hauv qab no:

RC7 (RX) ------------- TXD

RC6 (TX) ------------- RXD

GND -------------- NCO

Koj tsis tas yuav txuas VCC tus pin ntawm USB TTL hloov pauv (Txawm li cas los xij, yog tias koj xav tau koj tuaj yeem ua tau). Cov 3 kev sib txuas no txaus txaus.

Kauj Ruam 3: Python Software

Cia peb pib sau software rau Python sab uas yuav yog tus xa tawm hauv peb kis, vim tias nws yuav xa txoj hlua mus rau MCU.

import serial #import serial librarydata = '24' #data uas peb yuav xa cov ntaub ntawv = a+'\ 0' ser = serial. Serial ('COM17', baudrate = 9600, timeout = 1) #txuas mus rau qhov chaw nres nkoj ser.write (a.encode ()) #xa cov ntaub ntawv

Ua ntej tshaj plaws ntawm cov tsev qiv ntawv hauv xov tooj cua tau nkag los txhawm rau siv nws yav tom ntej. Peb xav xa cov piv txwv cov ntaub ntawv txhawm rau txhawm rau lees paub hauv MCU cov cai uas peb lees txais nws. Kuv xav nov sau ib yam. Qhov no yog vim li cas peb ntxiv '\ 0' rau txoj hlua. Qhov no vim tias, ntawm MCU sab nws tsis tuaj yeem nyeem cov hlua tag. Nws tau nyeem ib tus zuj zus. Yog li, peb xav paub qhov xaus ntawm txoj hlua txhawm rau txiav kev nyeem ntawv. Yog li, peb ntxiv '\ 0' rau txoj hlua uas qhia txog qhov kawg ntawm txoj hlua. Tom qab ntawd peb txuas mus rau qhov chaw nres nkoj uas txuas nrog MCU. Koj tuaj yeem txiav txim siab qhov chaw nres nkoj ntawd los ntawm kev tshawb nrhiav hauv 'Device Manager'. Yog li, ceev faj tias koj nyob ntawm qhov chaw nres nkoj tib yam. Tom qab tag nrho, peb xa cov ntaub ntawv mus rau MCU. ".encode ()" yuav tsum tau ntxiv rau cov ntaub ntawv hlua kom thiaj tuaj yeem xa nws mus rau tus txais.

Kauj Ruam 4: Microcontroller Software

Yog li, cia saib peb cov cai rau MCU. Ua ntej tshaj plaws kuv xav qhia koj cov ntaub ntawv "config.h" sab hauv uas tsis xav tau, tab sis kuv tau ua nws yooj yim. Nov tsuas yog hloov pauv zaus ntawm koj li MCU.

#ifndef CONFIG_H#txhais CONFIG_H

#suav nrog

#device ADC = 16

#FUSES NOWDT // Tsis Saib Tus Menyuam Timer

#FUSES NOBROWNOUT // Tsis muaj brownout pib dua #FUSES NOLVP // Tsis muaj qhov ntsuas hluav taws xob qis, B3 (PIC16) lossis B5 (PIC18) siv rau I/O

#siv ncua (siv lead ua = 6000000)

Tam sim no cia saib lub ntsiab cai:

#suav nrog

#suav nrog

#use rs232 (baud = 9600, xmit = pin_C6, rcv = pin_C7, parity = N, nres = 1)

#define LED_RED PIN_D0

kab inp; char cmp _ = "24"; char tsis [3];

#int_rda

void serial_communication_interrupt () {disable_interrupts (int_rda); unsigned int kuv = 0; inp = tau (); putc (pib); thaum (inp! = '\ 0') {tsis [kuv] = inp; inp = tau (); putc (pib); kuv ++; }}

void main (tsis muaj dab tsi) {

set_tris_d (0x00); output_d (0xFF); enable_interrupts (GLOBAL); thaum (1) {enable_interrupts (int_rda); yog (strcmp (tsis, cmp_) == 0) output_low (LED_RED); lwm qhov output_high (LED_RED); }}

Thaum pib peb suav nrog cov tsev qiv ntawv hlua uas peb yuav pab tau hauv txoj haujlwm ua haujlwm uas hauv peb cov ntaub ntawv yog txoj hlua sib piv ua haujlwm (strcmp). Yog li, peb lub hom phiaj hauv txoj cai no yog tig rau cov coj txuas nrog tus pin D0 yog tias tus nqi sib kis zoo ib yam li peb muab tus nqi uas yog "cmp_" sib npaug rau "24".

Ua ntej tshaj plaws peb pab cuam tshuam "rda" uas yuav ua rau cuam tshuam thaum cov ntaub ntawv xa mus.

Qhov thib ob, cia saib hauv ISR (cuam tshuam kev pabcuam niaj hnub) uas hu ua "serial_communication_interrupt". Hauv peb thawj zaug cuam tshuam cuam tshuam tus chij txhawm rau txhawm rau nyeem tus nqi tau txais thiab ua rau cuam tshuam ntxiv. Tom qab ntawd peb nyeem kab ntawv los ntawm tus cim kom mus txog '\ 0'. Thaum nyeem hauv kab peb kuj tseem sau txhua tus ntawv rau qhov tsis nyob hauv thiaj li yuav tau txais txoj hlua.

Thaum kawg, peb rov los sab hauv. Ntawm no peb sib piv peb txoj hlua tsis zoo uas tau txais txoj hlua thiab cmp_ hlua txhawm rau pom seb peb puas tau txais txoj hlua kom raug. Yog tias lawv sib npaug ces kuv tig rau coj, txwv tsis pub tua.*

*Hauv txoj cai kuv tau thim rov qab vim tias kuv lub rooj tsav xwm hloov pauv D chaw nres nkoj tus lej tus nqi. Hauv koj cov cai hloov nws mus rau:

yog (strcmp (tsis, cmp_) == 0) output_high (LED_RED); lwm qhov output_low (LED_RED);

Thaum kawg, sau nws thiab xa mus rau koj li MCU thiab tom qab ntawd sau cov cai hauv Python. Koj yuav tsum pom coj tig rau.

Kauj Ruam 5: Xaus

Peb tau ua tiav ib txoj haujlwm tiav. Kuv vam tias nws yuav muaj txiaj ntsig zoo rau koj. Yog tias koj muaj lus nug, thov tsis txhob ua siab deb nug:) Txog rau tom ntej no txoj haujlwm.

Pom zoo: