PIC Pictures

  • View
    129

  • Download
    1

Embed Size (px)

Text of PIC Pictures

(Embedded system) (ES304)

,

Microchip

PIC18F452

8255

82538251

SFR

RESET

RESET

OR

PIC-

- While(input(pin_b7)&&input(pin_b6)) { output_c (input_d()|0xe0); output_bit (pin_b0, input(!pin_c1)); }

-

-

-

- Analog to digital converter

-

-

SETUP_ADC_PORTs(); SETUP_ADC(); SET_ADC_CHANNEL();

READ_ADC();

-

-

-

-

1 A B ln R C (ln R)3 Rt max I ref 4V emax K 4V TC T 273 T

- #include "D:\Installs\PICC\Examples\abm.h #include math.h Float resistor, volt; void main() {

setup_adc_ports(RA0_RA1_ANALOG_RA3_REF); setup_adc(ADC_CLOCK_INTERNAL); do { while(input(pin_b1)); while(!input(pin_b1)); set_adc_channel(0); delay_us(12); resistor=read_adc(adc_start_and_read);set_adc_channel(1); delay_us(12); volt=read_adc(adc_start_and_read); processing(); }while(true); }

1 A B ln R C (ln R)3 TC T 273 T

processing() { float tempc, evolt, K, A, B, C, lnx; lnx=log(resistor); tempc=1/(A+B*lnx+C*lnx*lnx*lnx)-273; evolt=volt/K;

}

-

PIC16F877 3 - :

- Timer 0, timer 1, timer 2

PIC18F452 4 - : - timer 0, timer 1, timer 2, timer 3

-

SETUP_TIMERx(mode); value=GET_TIMERx();

SET_TIMERx(value);

-

-

0xFD5

-

-OxFCD

-

20000000Hz/4 5000000Hz ->T=200ns 200ns*4=800ns 800ns*0xC0=153.6us 153.6us*2=307.2us oscilloscope

0xFCA

-#include "D:\Installs\PICC\Examples\mba.h"

void main() {

20MHz

PIC 18F452PB0

setup_adc_ports(NO_ANALOGS); setup_adc(ADC_OFF); setup_psp(PSP_DISABLED); setup_spi(FALSE); setup_wdt(WDT_OFF); setup_timer_0(RTCC_INTERNAL); setup_timer_1(T1_DISABLED); setup_timer_2(T2_DIV_BY_4, 0xc0, 2); setup_timer_3(T3_DISABLED|T3_DIV_BY_1); enable_interrupts(INT_TIMER2); enable_interrupts(GLOBAL); while(true) { while(!t2if); output_high(pin_b0); output_low(pin_b0); } }

oscilloscope

307.2us-?

460.3us-?

-

setup_timer3(t3_internal | t3_div_by_2);#define T3_DISABLED 0 #define T3_INTERNAL 0x85 #define T3_EXTERNAL 0x87 #define T3_EXTERNAL_SYNC 0x83 #define T3_DIV_BY_1 #define T3_DIV_BY_2 #define T3_DIV_BY_4 #define T3_DIV_BY_8 0 0x10 0x20 0x30

or

0x85=10000101b 0x10=00010000b 0x95=10010101b

-

-#include "D:\Installs\PICC\Examples\mba1.h" void main() { int16 count=0; setup_adc_ports(NO_ANALOGS); setup_adc(ADC_OFF); setup_psp(PSP_DISABLED); setup_spi(FALSE); setup_wdt(WDT_OFF); setup_timer_0(RTCC_INTERNAL|RTCC_DIV_256); setup_timer_1(T1_DISABLED); setup_timer_2(T2_DISABLED,0,1); setup_timer_3(T3_EXTERNAL|T3_DIV_BY_1); disable_interrupts(global); while(true) { set_timer0(0xb3b5); //5000000:256=19531 set_timer3(0); t3on=true; while(!t0if); t3on=false; count=get_timer3(); } }

PIC

- - 1 2 1 2 I2C LCD LVD 0 SPI 0 1 2 3

FF1h

F9Fh7 6 5 4 3 2 1 0

FA2h7 6 5 4 3 2 1 0

18F452.h

#byte #byte #byte #bit #bit #bit .. #bit #bit INTCON2 0xFF1 IPR1 0xF9F IPR2 0xFA2 PSPIP ADIP T2IP T3IP CCP2IP 0xF9F.7 0xF9F.6 0xF9F.1 0xFA2.1 0xFA2.0

: timer0, timer3, ccp1, ad timer1,timer2, ccp2, psp

. . IPR1=0x44; IPR2=0x02; T3IP=true; //01***100b //******10b

18F452.h #define #define #define #define #define #define #define #define INT_TIMER0 INT_TIMER3 INT_RB INT_AD INT_RDA INT_CCP1 INT_LOWVOLT GLOBAL 0xF220 0xA002 0xF208 0x9D40 0x9D20 0x9D04 0xA004 0xF2C0

void main() { setup_adc_ports(RA0_ANALOG); ...... enable_interrupts(INT_TIMER0); enable_interrupts(INT_TIMER3); enable_interrupts(INT_RB); enable_interrupts(INT_AD); enable_interrupts(INT_RDA); enable_interrupts(INT_CCP1); enable_interrupts(INT_LOWVOLT); enable_interrupts(GLOBAL); }

FF2 INTCON

F9D PIE1

FA0 PIE2

#int_TIMER1 TIMER1_isr() { //Timer1- . } #int_EXT EXT_isr() { // (INTx) . } #int_AD AD_isr() { //- . } #int_RDA RDA_isr() { // // . }

- INTERRUPT 10MHz ADC TIMER1 Initialize temperature

Main Print: temperature

TIMER1 : start AD Conversion 104.8ms ADC end: temperature= RECEIVE command: send temperature to PC

- INTERRUPT

CCP module

CCP module

CCP module

CCP module

CCP module

CCP module

CCP module

CCP module

CCP module18F452.h :#define CCP_OFF #define CCP_CAPTURE_FE #define CCP_CAPTURE_RE #define CCP_CAPTURE_DIV_4 #define CCP_CAPTURE_DIV_16 #define CCP_COMPARE_SET_ON_MATCH #define CCP_COMPARE_CLR_ON_MATCH #define CCP_COMPARE_INT #define CCP_COMPARE_INT_AND_TOGGLE #define CCP_COMPARE_RESET_TIMER long CCP_1; #byte CCP_1 = #byte CCP_1_LOW= #byte CCP_1_HIGH= long CCP_2; #byte CCP_2 = #byte CCP_2_LOW= #byte CCP_2_HIGH= 0 4 5 6 7 8 9 0xA 0x2 0xB

0xFBE 0xFBE 0xFBF

0xFBB 0xFBB 0xFBC

CCP modulesetup_ccp1(ccp_capture_div_16); #define CCP_CAPTURE_DIV_16 07

setup_ccp2(CCP_COMPARE_CLR_ON_MATCH ); #define CCP_COMPARE_CLR_ON_MATCH 09

CCP module 1: Capture . (C ) - H_L ccp_1-

void main() { setup_timer_1(T1_INTERNAL | T1_DIV_BY_1); setup_ccp1(CCP_CAPTURE_FE); set_tris_c(0x4); // 00000100b

while(1) { output_b(ccp_1); output_d(ccp_1>>=8); } }

CCP module 2: Compare . _1 1- - .

void main() { setup_timer_1(T1_INTERNAL| T1_DIV_BY_8); setup_ccp1(CCP_COMPARE_SET_ON_MATCH); set_tris_c(0xFB); // 11111011b

ccp_1=0xFF; while(1) { // do nothing }}

20MHz 20MHz/4=5MHz; 5MHz/8=625KHz; 1.6us*255=408us;

CCP modulePulse Width Modulation

CCP modulePWM

CCP module

CCP module PWM

CCP modulesetup_ccp2(CCP_PWM_PLUS_1);

#define CCP_PWM #define CCP_PWM_PLUS_1 #define CCP_PWM_PLUS_2 #define CCP_PWM_PLUS_3 #define CCP_USE_TIMER3

0xC 0x1c 0x2c 0x3c 0x100

CCP module 3: PWM . CCP2- 100 k , 10-90 k . (Fosc=20MHz) Tosc=1/Fosc=1/20000000=0.00000005=50ns=50*10(-9)s 100us=(PR2+1)*4*Tosc*TMR2presc=|TMR2presc=4|= =(PR2+1)*16*50*10(-9) PR2=100*10(-6)/(16*50*10(-9))-1=100/800*10(3)-1= = 124=0x7C;

PWMduty=(CCPR1L:CCP1CON)*4*Tosc*TMR2prescPWMmin=10us=(CCPR1L:CCP1CON)*4*50*10(-9)*4; PWMmax=90us=(CCPR1L:CCP1CON)*4*50*10(-9)*4; (CCPR1L:CCP1CON)min=10*10(-6)/ (16*50*10(-9))= =0.0125*10(3)=12.5=0xC; (CCPR1L:CCP1CON)max=90*10(-6)/ (16*50*10(-9))= =0.0125*10(3)=112.5=0x70;

CCP modulevoid main() { int16 duty_cycle;setup_adc_ports(RA0_ANALOG); // 8 bit setup_adc(ADC_CLOCK_INTERNAL); setup_timer_2(T2_DIV_BY_4, 124, 1); setup_ccp2(ccp_pwm); set_tris_c(0x0); while(1) { duty_cycle=read_adc(adc_start_and_read); set_pwm2_duty(duty_cycle); } }

5V 0V 2.196V 0.235V

255=0xFF 0=0x0 112=0x70 12=0xC

PSP moduleParalel Slave Port

PSP moduleParalel Slave Port

PSP moduleParalel Slave Port

PSP moduleParalel Slave Port

PSP moduleParalel Slave Port

0xF96

0xFC1

PSP moduleParalel Slave Port

0xF96

PSP module

PSP modulePSP

PSP modulePSP

#define PSP_ENABLED #define PSP_DISABLED

0x10 0

#byte PSP_DATA

=

0xF83

// port D

PSP modulePSP

OBF IBF IBOV

PSP modulePSP #byte PSP_DATA 0xF83

// OBF bit

// IBF bit// port D register

// OVF bit

// port D register

PSP modulePSP

Int8 Int8 #int_PSP PSP_isr() {

data; error; //

if(psp_input_full ()) data=psp_read(); if (!psp_output_full()) psp_write(data); error=0xaa; }

display(int var ) { .. }

Main() {setup_psp(psp_enabled); ..... enable_interrupts(INT_PSP); enable_interrupts(GLOBAL); ...... if (error) display(error); display(data); }

MSSP Master Slave Synchronous Port

MSSP SPI mode

MSSP SPI mode

MSSP SPI mode

slave

master

MSSP SPI mode

0xFC6

0xFC6

0xFC5

MSSP

SPI_MASTER, SPI_SLAVE, SPI_SS_DISABLED SPI_L_TO_H, SPI_H_TO_L SPI_CLK_DIV_4, SPI_CLK_DIV_16, SPI_CLK_DIV_64, SPI_CLK_T2

MSSP - .

MSSP programming

MSSP programming

MSSP programming

MSSP programming

MSSP programming : int spi_dat; #INT_SSP ssp_isr() { spi_dat=spi_read(); } // SPI or I2C activity

Main() { set_spi(spi_master | spi_l_to_h | spi_clk_div_4); ..... enable_interrupts(INT_SSP ); enable_interrupts(GLOBAL); ...... }

MSSP programming

MSSP programming#define zero 0x3f #define one 0x6 . #define nine 0x6f #include #include

int code[4]; int fig[]={zero,one, , nine, , fifteen};

bin2hex(long x) { int n=0; ldiv_t lidiv; while(x>15) { lidiv=ldiv(x, 16); code[n]=lidiv.rem; n++; x=lidiv.quot; } code[n]=x; }

MSSP programming

display() { int i; for(i=3; i>=0; i--) spi_write(fig[code[i]]);

}

MSSP programmingclear_code() { int k; for(k=0; k