1 / 33

Embedded Systems “PIC Microcontroller and Embedded Systems”

Embedded Systems “PIC Microcontroller and Embedded Systems”. Chapter 7: PIC Programming in C. 7.1 Data Type and Time Delays in C. C data types for the PIC18. 7.1 Data Type and Time Delays in C (2). Unsigned char Example 7-1 Write a C18 program to send values 00-FF to Port B Example 7-2

Download Presentation

Embedded Systems “PIC Microcontroller and Embedded Systems”

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Embedded Systems“PIC Microcontroller and Embedded Systems” Chapter 7: PIC Programming in C

  2. 7.1 Data Type and Time Delays in C • C data types for the PIC18 PIC Microcontroller and Embedded Systems

  3. 7.1 Data Type and Time Delays in C (2) • Unsigned char • Example 7-1 • Write a C18 program to send values 00-FF to Port B • Example 7-2 • Write a C18 program to send values for ASCII characters of 0,1,2,3,4,5,A,B,C,D to Port B • Example 7-3 • Write a C18 program to toggle all the bits of Port B continuously PIC Microcontroller and Embedded Systems

  4. 7.1 Data Type and Time Delays in C (3) • Unsigned char (cont’d) • In declaring variables, we must pay careful attention to the size of the data and try to use unsigned char instead of int if possible. • PIC18 microcontrollers have a limited number of registers and data RAM locations. PIC Microcontroller and Embedded Systems

  5. 7.1 Data Type and Time Delays in C (4) • Signed char • If unsigned is not used, the default is the signed value • Example 7-4 • Write a C-18 program to send values -4 to +4 to Port B PIC Microcontroller and Embedded Systems

  6. 7.1 Data Type and Time Delays in C (5) • Unsigned int • The C18 is an 8-bit microcontroller, the int data type takes two bytes of RAM. • Use chars instead of ints when that is possible • Use unsigned instead of signed when that is possible • Signed int .. Other data types • Example 7-5 • Write a C-18 program to toggle all bits of Port B 50,000 times • Example 7-6 • Write a C-18 program to toggle all bits of Port B 100,000 times PIC Microcontroller and Embedded Systems

  7. 7.1 Data Type and Time Delays in C (6) • Time delay • Can be done by: • Using a simple for loop • Using the PIC18 timers • When using the for loops, two factors affect the time delay: • Crystal frequency • Compiler used… • Example 7-7 • Write a C18 program to toggle all the bits of PORT B continuously with a 250 ms delay. Assume that the system has a XTAL = 10 MHz. PIC Microcontroller and Embedded Systems

  8. 7.1 Data Type and Time Delays in C (7) • Example 7-8 • Write a C18 program to toggle all the bits of PORT C and PORT D continuously with a 250 ms delay. PIC Microcontroller and Embedded Systems

  9. 7.2 I/O Programming in C • Byte size I/O • Example 7-9 • LEDs are connected to bits in Port B and Port C. Write a C18 program that shows the count from 0 to $FF on the LEDs • Example 7-10 • Write a C18 program to get a byte of data from Port B, wait ½ second, and then sent it to Port C • Example 7-11 • Write a C18 program to get a byte of data from Port C. If it is less than 100, send it to Port B; otherwise, send it to Port D. PIC Microcontroller and Embedded Systems

  10. 7.2 I/O Programming in C (2) • Bit-addressable I/O programming • The ports are bit addressable. We can access a single bit without disturbing the rest of the port. • PORTxbits.Rxy to access the y bit of Port x PIC Microcontroller and Embedded Systems

  11. 7.2 I/O Programming in C (3) PIC Microcontroller and Embedded Systems

  12. 7.2 I/O Programming in C (4) • Example 7-12 • Write a C18 program to toggle only bit RB4 continuously without disturbing the rest of the bits of Port B • Example 7-13 • Write a C18 program to monitor bit PC5. If it is HIGH, send 0x55 to Port B; otherwise, send 0xAA to Port D. PIC Microcontroller and Embedded Systems

  13. 7.2 I/O Programming in C (5) • Example 7-14 • A door sensor is connected to the RB1 pin, and a buzzer is connected to RC7. Write a C18 program to monitor the door sensor, and when it opens, sound the buzzer. You can sound the buzzer by sending a square wave of a few hundred Hz frequency to it. PIC Microcontroller and Embedded Systems

  14. 7.2 I/O Programming in C (6) • Example 7-15 • The data pins of an LCD are connected to Port B. The information is latched into the LCD whenever its Enable pin goes from HIGH to LOW. Write a C18 program to send a message to this LCD. PIC Microcontroller and Embedded Systems

  15. 7.2 I/O Programming in C (7) • Example 7-16 • Write a C18 program to toggle all the bits of Port B, Port C, and Port D continuously with a 250 ms delay. • Example 7-17 • Write a C18 program to turn bit 5 of Port B on and off 50,000 times • Example 7-18 • Write a C18 program to get the status of bit RB0, and send it to RC7 continuously. PIC Microcontroller and Embedded Systems

  16. 7.3 Logic Operations in C • Bitwise logic operators in C • Differ from regular logic operators such as &&, II, ! • 0x35 & 0x0F = 0x05 0x04 | 0x68 = 0x6C • 0x54 ^ 0x78 = 0x2C ~0x55 = 0xAA • Bitwise shift operations in C • Shift right (>>): data >> # of bits to shift, 0x9A >> 3 = 0x13 • Shift left (<<): data >> # of bits to shift, 0x6 << 4 = 0x60 PIC Microcontroller and Embedded Systems

  17. 7.3 Logic Operations in C (2) • Example 7-19 • Example logic operations • Example 7-20 • Write a C18 program to toggle all the bits of Port B and Port C continuously with a 250 ms delay. Use the inverting operator. • Example 7-21 • Rewrite the C18 program to toggle all the bits of Port B, Port C, and Port D continuously with a 250 ms delay. Use the EX-OR operator • Example 7-22 • Rewrite the C18 program to get bit RB0 and send it to RC7 after inverting it. PIC Microcontroller and Embedded Systems

  18. 7.3 Logic Operations in C (3) • Example 7-23 • Write a C18 program to read the RB0 and RB1 bits and issue an ASCII character to PD according to the following table: • RB1 RB0 • 0 0 send ‘0’ to PORTD (ASCII ‘0’ is 0x30) • 0 1 send ‘1’ to PORTD • 1 0 send ‘2’ to PORTD • 1 2 send ‘3’ to PORTD PIC Microcontroller and Embedded Systems

  19. 7.4 Data Conversion Programs in C • ASCII numbers • Convert a packed BCD (stored value) to ASCII (Print or display) PIC Microcontroller and Embedded Systems

  20. 7.4 Data Conversion Programs in C (2) • Example 7-24 • Write a C18 program to convert packed BCD 0x29 to ASCII and display the bytes on PORTB and PORTC • Example 7-25 • Write a C18 program to convert ASCII digits of ‘4’ and ‘7’ to packed BCD and display it on PORTB PIC Microcontroller and Embedded Systems

  21. 7.4 Data Conversion Programs in C (3) • Assume that we have 4 bytes of hexadecimal data: 0x25, 0x62, 0x3F, and 0x52. • A) Find the checksum byte (add the bytes, drop the carries, take the 2’s complement of the total) • 0x25 + 0x62 + 0x3F + 0x52 = 0x118 • Drop carries: 0x18 • 2’s complement: 0xE8 (this is the checksum byte) • B) Perform the checksum operation to ensure data integrity (on the receiver side) (add all bytes including the checksum byte, drop the carries, the result should be 0) • 0x25 + 0x62 + 0x3F + 0x52 + 0xE8 = 0x200 (0 after dropping the carries) • Data is not corrupted PIC Microcontroller and Embedded Systems

  22. 7.4 Data Conversion Programs in C (4) • C) If there is an error (0x62 has been changed to 0x22) • 0x25 + 0x22 + 0x3F + 0x52 + 0xE8 = 0x1C0 (0xC0 after dropping the carries) • The final result is not 0, this detects the error. • Example 7-27 • Write a C18 program to calculate the checksum byte for the data given in the example • Example 7-28 • Write a C18 program to perform step B). If the data is good, send ASCII character ‘G’ to PORTD; otherwise, send ‘B’ PIC Microcontroller and Embedded Systems

  23. 7.4 Data Conversion Programs in C (5) • Binary (hex) to decimal and ASCII conversion in C18 • Example 7-29 • Write a C18 program to convert 11111101 (0xFD) to decimal and display the digits on PORTB, PORTC, and PORTD PIC Microcontroller and Embedded Systems

  24. 7.5 Data Serialization in C • Can be done by using a serial port (dealt with later in the course) or by sending data one bit at a time and control the sequence of data and the spaces between them • Example 7-30 • Write a C18 program to send out the value 0x44 serially one bit at a time via RC0. The LSB should go out first • Example 7-31 • Write a C18 program to send out the value 0x44 serially one bit at a time via RC0. The MSB should go out first. PIC Microcontroller and Embedded Systems

  25. 7.5 Data Serialization in C (2) • Example 7-32 • Write a C18 program to bring in a byte of data serially one bit at a time via the RB0 pin. Place the byte on PORTD. The LSB should come in first. • Example 7-33 • Write a C18 program to bring in a byte of data serially one bit at a time via the RB0 pin. The MSB should come in first. PIC Microcontroller and Embedded Systems

  26. 7.6 Program ROM Allocation in C18 • RAM data space vs. code data space • In the PIC18 we have two spaces in which to store data • The 4096 bytes of data RAM space with address range 0x000-0xFFF. Many PIC18 chips have much less than 4096 bytes for the file register data RAM • The 2M of code (program) space with addresses of 0x000000 – 0x1FFFFF. This 2M of on-chip ROM space is used for storing programs and thus is directly under the control of the PC. Many PIC18 chips have much less than the 2M. • The more code space is used for data, the less is left for the code itself. • To alleviate this problem, Microchip added an EEPROM to be used for data storage especially for applications that require a large amount of data (look-up tables…. ) PIC Microcontroller and Embedded Systems

  27. 7.6 Program ROM Allocation in C18 (2) • PIC18 Program ROM Space PIC Microcontroller and Embedded Systems

  28. 7.6 Program ROM Allocation in C18 (3) • Allocating program space to data • In all our C18 examples so far, the data were stored in the data RAM. • It is common practice to use the on-chip program ROM for the purpose of storing fixed data such as strings. This is especially useful since we have limited amount of file register data RAM. • Use the rom keyword to make the compiler use the ROM • Program 7-1 PIC Microcontroller and Embedded Systems

  29. 7.6 Program ROM Allocation in C18 (4) • NEAR and FAR for code • Near: ROM: in program space of 0000 – 0xFFFF (64KB) • Far: ROM: in program space of 000000 – 0x1FFFFF (2MB) • Program 7-2A • Program 7-2B PIC Microcontroller and Embedded Systems

  30. 7.6 Program ROM Allocation in C18 (5) • Pragma and allocating a fixed address to data and code • Putting code in a specific ROM address • #pragma code “name” = “location” • Program 7-3 • Putting data in a specific ROM address • #pragma romdata “name” = “location” • Program 7-4 PIC Microcontroller and Embedded Systems

  31. 7.7 Data RAM Allocation In C18 • RAM data space usage by the C18 compiler • Up to 4K RAM (not all members of the family and some chips quite less). • 128 bytes are used for SFRs and remaining RAM for the scratch pad • Program 7-5 Data RAM space: x, y, z PIC Microcontroller and Embedded Systems

  32. 7.7 Data RAM Allocation In C18 (2) • Program 7-6 • Program 7-7 PIC Microcontroller and Embedded Systems

  33. 7.7 Data RAM Allocation In C18 (3) • NEAR and FAR for data • Near: RAM: In access bank • Far: RAM: Anywhere in data RAM file register (default) • Program 7-8A • Program 7-8B • Putting data in a specific RAM address • #pragma idata for initialized data • Program 7-9 • #pragma udata for uninitialized data • Program 7-10 • Using both • Program 7-11 PIC Microcontroller and Embedded Systems

More Related