c compilers for 8051 chin-shiuh shih. c compilers for 8051 –sdcc - small device c compiler...

15
C compilers for 8051 Chin-Shiuh Shih

Upload: ursula-sherman

Post on 12-Jan-2016

253 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: C compilers for 8051 Chin-Shiuh Shih. C compilers for 8051 –SDCC - Small Device C Compiler –Raisonance RIDE-51 –Keil –… Some issues in using 8051 C compilers

C compilers for 8051

Chin-Shiuh Shih

Page 2: C compilers for 8051 Chin-Shiuh Shih. C compilers for 8051 –SDCC - Small Device C Compiler –Raisonance RIDE-51 –Keil –… Some issues in using 8051 C compilers

C compilers for 8051

• C compilers for 8051– SDCC - Small Device C Compiler– Raisonance RIDE-51– Keil– …

• Some issues in using 8051 C compilers– Access to SFR (Special Function Register)– Implement Interrupt Service Subroutine– Memory allocation in variable declaration– …

Page 3: C compilers for 8051 Chin-Shiuh Shih. C compilers for 8051 –SDCC - Small Device C Compiler –Raisonance RIDE-51 –Keil –… Some issues in using 8051 C compilers

SDCC - Small Device C Compiler

• SDCC - Small Device C Compiler is a free C compiler for 8051, although library is incomplete.

• http://sdcc.sourceforge.net

• Download sdcc-2.8.0-setup.exe and install.

Page 4: C compilers for 8051 Chin-Shiuh Shih. C compilers for 8051 –SDCC - Small Device C Compiler –Raisonance RIDE-51 –Keil –… Some issues in using 8051 C compilers

• Issue "sdcc test.c" within DOS box will generate "test.ihx" in Intel-HEX format.

• Issue "sdcc --code-loc 0x4000 --xram-loc 0x8000 test.c" within DOS box will generate "test.ihx" in Intel-HEX format.– Option "--code-loc 0x4000" is used to specify

starting code address.– Option "--xram-loc 0x8000" is used to specify t

he starting address of external data memory.

Page 5: C compilers for 8051 Chin-Shiuh Shih. C compilers for 8051 –SDCC - Small Device C Compiler –Raisonance RIDE-51 –Keil –… Some issues in using 8051 C compilers

• Refer to "c:\sdcc\share\doc\sdcc\sdccman.pdf" for SDCC Compiler User Guide.

• uart.c is a UART library by Chin-Shiuh Shieh, including Character, String, and Integer I/O. Refer to sample.c for its usage.

Page 6: C compilers for 8051 Chin-Shiuh Shih. C compilers for 8051 –SDCC - Small Device C Compiler –Raisonance RIDE-51 –Keil –… Some issues in using 8051 C compilers

// Template for SDCC 8051 C Language

#include <at89x51.h> // Definitions of registers, SFRs and Bits

// ISR Prototypes ===================================================void INT0_ISR(void) interrupt 0; // ISR for External Interrupt 0void T0_ISR(void) interrupt 1; // ISR for Timer0/Counter0 Overflowvoid INT1_ISR(void) interrupt 2; // ISR for External Interrupt 1void T1_ISR(void) interrupt 3; // ISR for Timer1/Counter1 Overflowvoid UART_ISR(void) interrupt 4; // ISR for UART Interrupt

void main(void){}

void INT0_ISR(void) interrupt 0{}void T0_ISR(void) interrupt 1{}void INT1_ISR(void) interrupt 2{}void T1_ISR(void) interrupt 3{}void UART_ISR(void) interrupt 4{}

Page 7: C compilers for 8051 Chin-Shiuh Shih. C compilers for 8051 –SDCC - Small Device C Compiler –Raisonance RIDE-51 –Keil –… Some issues in using 8051 C compilers

• Input– P1=0xFF;– A=P1;

• Output– P2=0xF5;

Page 8: C compilers for 8051 Chin-Shiuh Shih. C compilers for 8051 –SDCC - Small Device C Compiler –Raisonance RIDE-51 –Keil –… Some issues in using 8051 C compilers

• Check button in assemblyCHECK_BUTTON:JB P3.2,CBRET; Task for button pressedWAIT_BUTTON_RELEASE:JNB P3.2,WAIT_BUTTON_RELEASECBRET:

• Check button in Cif(P3_2==0){…while(P3_2==0);

}

Page 9: C compilers for 8051 Chin-Shiuh Shih. C compilers for 8051 –SDCC - Small Device C Compiler –Raisonance RIDE-51 –Keil –… Some issues in using 8051 C compilers

• Wait button in assemblyWAIT_BUTTON:

JB P3.2,WAIT_BUTTON

WAIT_RELEASE:

JNB P3.2,WAIT_RELEASE

• Wait button in Cwhile(P3_2==1);

while(P3_2==0);

Page 10: C compilers for 8051 Chin-Shiuh Shih. C compilers for 8051 –SDCC - Small Device C Compiler –Raisonance RIDE-51 –Keil –… Some issues in using 8051 C compilers

• Left Rotation10010011 00100111

• Left Rotation in assemblyRL A

• Left Rotation in Cunsigned char i;

if(i>=128)

i=i*2+1;

else

i=i*2;

Page 11: C compilers for 8051 Chin-Shiuh Shih. C compilers for 8051 –SDCC - Small Device C Compiler –Raisonance RIDE-51 –Keil –… Some issues in using 8051 C compilers

• Right Rotation10010011 11001001

• Right Rotation in assemblyRR A

• Right Rotation in Cunsigned char i;

if(i%2==1)

i=i/2+128;

else

i=i/2;

Page 12: C compilers for 8051 Chin-Shiuh Shih. C compilers for 8051 –SDCC - Small Device C Compiler –Raisonance RIDE-51 –Keil –… Some issues in using 8051 C compilers

• data unsigned char test_data;• xdata unsigned char test_data;• xdata at 0x7ffe unsigned int chksum;• idata unsigned char test_data;

• code unsigned char test_code;

• bit test_bit;

Page 13: C compilers for 8051 Chin-Shiuh Shih. C compilers for 8051 –SDCC - Small Device C Compiler –Raisonance RIDE-51 –Keil –… Some issues in using 8051 C compilers

• xdata at 0x0000 unsigned char ADC;

• ADC=0;

• X=ADC;

Page 14: C compilers for 8051 Chin-Shiuh Shih. C compilers for 8051 –SDCC - Small Device C Compiler –Raisonance RIDE-51 –Keil –… Some issues in using 8051 C compilers
Page 15: C compilers for 8051 Chin-Shiuh Shih. C compilers for 8051 –SDCC - Small Device C Compiler –Raisonance RIDE-51 –Keil –… Some issues in using 8051 C compilers

• xdata at 0x3000 unsigned char x[1024];

• for(i=0;i<1024;i++)– x[i]=i;