Introduction to Embedded Programming Concepts and Cross Compiler Usage

introduction to embedded programming concepts n.w
1 / 30
Embed
Share

Explore the world of embedded systems and programming with a focus on C language. Learn about the applications, advantages of using C language, and the role of cross compilers in programming microcontrollers. Dive into a simple embedded C program example to understand practical implementation.

  • Embedded Systems
  • Programming Concepts
  • Cross Compiler
  • C Language
  • Microcontrollers

Uploaded on | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. INTRODUCTION TO EMBEDDED PROGRAMMING CONCEPTS 4/27/2025 MET's IOE, BKC 1

  2. Embedded systems What are we talking about ? Fuel injector controls Medical equipment monitors Printer controllers Sound systems Rice cookers Telephone switches Water pump controllers Welding machines Windmills Wrist watches Assembly line quality monitors Bar code readers Bread machines Cameras Car assembly robots Cell phones CD players Disk drive controllers 4/27/2025 MET's IOE, BKC 2

  3. Introduction to Embedded Programming in C The main concern in microcontroller programming is program memory space Assembly language program produces HEX file C Language is used in Microcontrollers for following advantages: Programming in C is less tedious and less time consuming It is easier to Modify & Update It is portable to other Microcontrollers with some modification C also provides some library functions Compiler is used to convert C Language i.e HLL into Binary Language i.e Machine Language 4/27/2025 MET's IOE, BKC 3

  4. Cross Compiler For Microcontrollers, Pentium Processor based Compiler is used to do Programming Pentium Processor based Compiler has to make a code of PIC18F877 from C Programming Language , such compiler is called as CrossCompiler The widely used Cross Compiler for PIC18 & many other microcontroller of PIC series is MPLAB MPLAB has Header File for every Microcontroller The header file contains variable declaration, macro definition, & definition for special functions used.. The Header File for PIC 16F877 is 16F877A.h 4/27/2025 MET's IOE, BKC 4

  5. Simple Embedded C Program The program uses header file as 16F877A.h The Header File has description of all the Registers of corresponding Microcontroller The main( ) function is must in Embedded C #include 16F877A.h main( ) is Entry point of program The Function output_C() is used to output a data to a port C specified in function itself void main( ) { output_C(85); } The value to be given to the port is passed as parameter to this function in the brackets In this case the parameter passed is 85 which is decimal number The n=binary equivalent of this number will be (01010101)2 So alternate 1 s & 0 s will be output on port C 4/27/2025 MET's IOE, BKC 5

  6. Embedded C Programming Basics We use variables in the programs to store temporary data e.g We will try to take a data from port A and give the same to port B i.e the data has to be taken from port A into a variable say x and the value of this variable i.e x is to be given to port B as an Output So in this case use of Data Types is very important in case of Embedded C Programming The data types must be properly used based on the range required so as to have compact programs This will assure less memory space for program and data, which is a very important characteristics of Microcontroller based embedded systems 4/27/2025 MET's IOE, BKC 6

  7. Data Types used in Embedded C Data Type Size in bits Range or Usage int1 unsigned int8 signed int8 unsigned int16 signed int16 unsigned int32 signed int32 1 8 8 0 to 1 0 to 255 -128 to +128 0 to 65535 -32767 to 32768 0 to 4294967296 -2147483648 to 2147483647 16 16 32 32 4/27/2025 MET's IOE, BKC 7

  8. Floating Point Data Format for PIC Microcontroller The PIC microcontroller in its Embedded C program uses a 32-bit format of representation This representation has : 1 bit for Sign, 8 bit for Exponent 23 bits for Mantissa Exponent Sign Mantissa XXXX XXXX X XXX XXXX XXXX XXXX XXXX XXXX 8 bits 1 23 bits Fig:32-bit Floating point representation for MPLAB 4/27/2025 MET's IOE, BKC 8

  9. Another Embedded C Program The program uses header file as 16F877A.h One major thing to note here is that int type data uses 8-bit by default , as it is a 8 bit Microcontroller , when it is referred to Embedded C #include 16F877A.h void main( ) { int x; x=input_A( ); output_B(x); } We have used a function input_A( ), to read a data from PORT A into variable x Function input_A( ), is used to give data to PORT B from variable x 4/27/2025 MET's IOE, BKC 9

  10. For Loop Embedded C Example using FOR Loop #include 16F877A.h void main() { int x; for(x=1;x<=10;x++) { output_B(x); delay_ms(100); } } Here a new function is used in program i.e delay_ms( ) This function calls a delay which generates a delay between the two statements of milliseconds specified in brackets i.e parameters are passed to function 4/27/2025 MET's IOE, BKC 10

  11. Do - While Loop Embedded C Example using DO-WHILE Loop WAP to display 1 to 10 numbers on output port B #include 16F877A.h void main() { int x; x=1; do { output_B(x); delay_ms(100); x++; } while(x<=10) } 4/27/2025 MET's IOE, BKC 11

  12. While Loop Embedded C Example using WHILE Loop #include 16F877A.h void main() { int x; x=1; while(x<=10) { output_B(x); x++; } } 4/27/2025 MET's IOE, BKC 12

  13. Functions available in MPLAB compiler Following set of functions used to perform some basic operations in MPLAB Compiler/Toolkit The functions are output_x( ), input_x( ) etc Let us look at all those instructions : Sr No Library Function Operation done by the function 1 delay_us(x) This function generates a delay of x microsecond i.e. a delay of microseconds equal to the parameter passed to it. 2 delay_ms(x) This function generates a delay of x milliseconds i.e. a delay of milliseconds equal to the parameter passed to it. 3 output_p(x) This function outputs the parameter x passed to it to the output port specified by character p This function sets the x pin of the p port to logic 1 4 output_high(PIN_px) 5 output_low(PIN_px) This function sets the x pin of the p port to logic 0 4/27/2025 MET's IOE, BKC 13

  14. Contd Sr No Library Function Operation done by the function 6 input_x() This function returns the value on input port x 7 input(PIN_px) This function returns the value of x pin of the p port 4/27/2025 MET's IOE, BKC 14

  15. Operators Unary Operator ( ): Logical Not Operator (!): if (x==1) then y = !x ; // y = 0 Bitwise not Operator (~): Increment / decrement Operator (x++, ++x, x , x) Binary Operators: Operators that require two operands. Arithmetic Operators: * , / , %, +, . Bitwise Operators: AND, OR, NOT etc. ~ NOT operators & AND operator : 5 & 3 = 1 (5)10 = ( 0 1 0 1)2 (3)10 = ( 0 0 1 1)2 y = x; if x==3 then y= ~x; // y= 3 ( 0 0 0 1)2 = (1)10 | OR operator : 12 | 9 = 13 (12)10 = ( 1 1 0 0)2 (9)10 = ( 1 0 0 1)2 4/27/2025 ( 1 1 0 1)2 =(13)10 MET's IOE, BKC 15

  16. Operators Binary Operators: Operators that require two operands. ^ EXOR operator : 8 ^ 10 = 2 (8)10 = ( 1 0 0 0)2 (10)10 = ( 1 0 1 0)2 ( 0 0 1 0)2 = (2)10 << Left Shift operator : 10 << 2 = 40 After shifting left once ( 0 0 0 1 0 1 0 0)2 After shifting left twice ( 0 0 1 0 1 0 0 0)2 = (40)10 (10)10 = ( 0 0 0 0 1 0 1 0)2 >> Right Shift operator : 13 << 3 = 1 After shifting Right once ( 0 0 0 0 0 1 0 1)2 After shifting Right twice ( 0 0 0 0 0 0 1 0)2 After shifting Right thrice ( 0 0 0 0 0 0 0 1)2 = (1)10 (13)10 = ( 0 0 0 0 1 0 1 1)2 16 4/27/2025 MET's IOE, BKC

  17. Global Variables and Local Variables Local Variables: Scope of variables is limited to function in which it is called. Life of such variables is till control remains in particular function where it is defined. Initial value of such variables is garbage. Global variables: Scope of variable is accessible from any where in the program. Life of such variable is till program is alive. Used when a variable is used in multiple functions. Initial value of such variable is zero. 17 4/27/2025 MET's IOE, BKC

  18. Functions, Pointers, Arrays, String and Enumeration Functions , Recursive functions Referencing and De-referencing (Operators in pointers) Address of operator ( & ) &x = 3000 Value of Operator ( * ) *x = 12 Arrays: int a[10] => a[0] = 10, a[1] = 20, a[3] = 30 . Multidimensional Array : int a[10][10] => a[0][0] = 10, a[0][1] = 20 Strings : array of characters 18 4/27/2025 MET's IOE, BKC

  19. Introduction to C Peripheral Interface PIC ADC The ADC of PIC18F877 has 8 analog input channels , and single channel is of 10-bit The PORT A pins are RA0 - RA5 The PORT E pins are RE1 RE2 i.e Analog Input Pins are AIN0 to AIN7 To select ADC Channel set_ADC_channel(n) is used for i/p ADC Channel Another function i.e read_ADC( ) is used to read the data from analog to digital convertor We can also measure voltage level using ADC 4/27/2025 MET's IOE, BKC 19 Fig : ADC related function of PIC18F877

  20. Contd.. PIC Interrupts #int_<interrupt_name> e.g : #int_ext There is a directive to indicate an Interrupt Service Routine ISR as: Fig : Interrupt related function of PIC18F877 4/27/2025 MET's IOE, BKC 20

  21. Fig: Interrupt sources in PIC18F877 MET's IOE, BKC 4/27/2025 21

  22. PIC Timers & Counters We can program the timer counters of PIC microcontroller It can be working either as timer OR as counter and hence have the two functions To keep a track of time To keep a count of number of times an event occurs It also helps generating delays The timer/counter can also help in multiple operations such as compare, capture, PWM(Pulse Width Modulation) 4/27/2025 MET's IOE, BKC 22

  23. Introduction to C Peripheral Interface PIC Timers and Counters Timer function of timer/counter keeps track of timer or helps in generating delays. Counter mode of operation of a timer/counter helps in counting no. of times an event occurs. Can used for compare, capture as well as PWM (Pulse Width Modulation) Sr. Interrupt Source interrupt_name 1. setup_timerx(<mode>) Select the mode of timer x 2. get_timerx() Reads the content of timer register of corresponding timer x 3. set_timerx(n) Loads value n intimer x 4. setup_ccpx(ccp_pwm) Sets timer x in CCP mode of PWM 5. setup_ccpx(ccp_capture_re) Set timer x in CCP mode of capture mode with rising edge. 6. setup_ccpx(ccp_compare) Set timer x in CCP mode of compare mode. MET's IOE, BKC 23 4/27/2025

  24. PIC UART Programming A universal asynchronous receiver/transmitter, abbreviated UART , is a piece of computer hardware that translates data between parallel and serial forms Baud Rate: It is the unit of symbol rate, also known as baud or modulation rate; the number of distinct symbol changes (signaling events) Sr No Function Name Operation of Function 1 setup_uart(n) The function sets the baud rate of UART port to the value given Baud Rate: It is the unit of symbol rate, also known as baud or modulation rate; the number of distinct symbol changes (signaling events) This function write a character to the default port i.e serial port 2 putc(n) 3 getc() This function reads a character i.e data received on serial port Fig : RS232 functions of MPLAB Compiler MET's IOE, BKC 4/27/2025 24

  25. SPI Serial Bus SPI :Serial Peripheral Interface normally preferred for program transfer and other huge data communication from microcontroller Sr No Function Name Operation of Function 1 setup_spi(spi_master) or setup_spi(spi_slave) The function initializes the SPI port 2 spi_read() This function is used to accept the data received by SPI port/eg. X=spi_read(); 3 spi_write(n) This function is used to transfer a character byte n Fig : SPI functions in MPLAB 4/27/2025 MET's IOE, BKC 25

  26. Parallel Slave Port A Parallel Slave Port (PSP) is an interface found on some PIC microcontrollers. It allows 8-bit asynchronous bidirectional transfer between the PIC and external devices, such as other microcontrollers computers. data or personal 4/27/2025 MET's IOE, BKC 26 Table: Functions of PSP in MPLAB

  27. PIC I2 C Serial Port This communications microcontroller is yet another supported type of by serial PIC I C (Inter-Integrated Circuit, referred to as I- squared-C, I-two-C, or IIC) is a multimaster serial single-ended computer bus It is used for attaching low-speed peripherals to a motherboard, embedded system, cellphone, or other digital electronic devices Fig : I2C Library functions in MPLAB 4/27/2025 MET's IOE, BKC 27

  28. C Mechatronics Applications The PICDEM mechatronics demonstration board, supplied by Microchip Inc., is a very useful target system for C control applications It is built around a PIC 16F917 but incorporates an LCD driver module, which allows a plain display 3.5-digit The MCU internal clock runs at 8 MHz, giving a 0.5-s instruction cycle. The main output devices are a small DC motor and a stepper motor Fig: PICDEM Mechatronics Board (by permission of Microchip Inc.) An RS232 serial port for exchanging data with the PC host is fitted 4/27/2025 MET's IOE, BKC 28

  29. Mechatronics applications of the PIC OR any Embedded Systems mainly involves driving motors (like stepper motors, DC motor, servo motors etc.), interfacing displays (like LCD or LED), interfacing sensors etc The motor is such connected that the arrangement of the switches will decide the direction of the motor s shaft rotation As shown in figure , when the switches S1 and S4 are closed, the motor s shaft will move in one direction and when the switches S2 and S3 are closed, it will move in the other direction. Fig : Interfacing DC Motor , H-bridge for motor driving 4/27/2025 MET's IOE, BKC 29

  30. Note: The sequence of the bits to be given for full stepping in a stepper motor is: 09H, 0AH, 06H, 05H in reverse direction The sequence of the bits to be given for half stepping in a stepper motor is: 09H, 08H, 0AH, 02H, 06H, 04H, 05H, 01H, 09H in reverse direction 4/27/2025 MET's IOE, BKC 30

Related


More Related Content