Difference between revisions of "ME333 Unofficial Sample Code"

From Mech
Jump to navigationJump to search
(Created page with "== A Crash Course in C == '''Section A.1''': HelloWorld.c '''Section A.4''': printout.c, [[Media:CCCdatasizes.c|datasizes...")
 
 
(32 intermediate revisions by 4 users not shown)
Line 1: Line 1:
'''OBSOLETE. THIS PAGE REFERS TO A PRE-RELEASE VERSION OF THE NU32 PIC32 DEVELOPMENT BOARD. FOR INFORMATION, SAMPLE CODE, AND VIDEOS RELATED TO THE PRODUCTION VERSION (2016 AND LATER), AND TO THE CORRESPONDING BOOK "EMBEDDED COMPUTING AND MECHATRONICS WITH THE PIC32 MICROCONTROLLER," VISIT [[NU32|THE NU32 PAGE]].'''
== A Crash Course in C ==


'''Section A.1''': [[Media:CCCHelloWorld.c|HelloWorld.c]]


Code for ME333 instructors to test before it goes live.
'''Section A.4''': [[Media:CCCprintout.c|printout.c]], [[Media:CCCdatasizes.c|datasizes.c]], [[Media:CCCoverflow.c|overflow.c]], [[Media:CCCtypecast.c|typecast.c]], [[Media:CCCinvest.c|invest.c]]


Code with '''TESTED''' has been tested and is ready to go live.
'''Section A.4.16''': [[Media:CCChelper.h|helper.h]], [[Media:CCChelper.c|helper.c]], [[Media:CCCmain.c|main.c]]


Code with '''EDITS''' has been updated but not fully test.
'''Problems, Section A.5''': [[Media:CCCbubble.c|bubble.c]], [[Media:CCClights.c|lights.c]]


Code without any marking has not been looked at yet.
== Chapter 5 ==


[[Media:Ch5_timing.c|timing.c]]


== PIC32 Code ==
== PIC32 Code ==


'''Bootloader Project'''
'''DIP PIC32 with reconfigurable pins (1/2xx)''' (11/21)
* '''1/30/2013''' [[Media:NU32_bootloader.zip|NU32_bootloader.zip]]: (Updated 1/30 to make it jump to the noncacheable virtual address, not cacheable, just for conceptual clarity.) All files in one folder. Kevin's version building on Nick's. Jumps to application at new jump location that doesn't waste flash. Also sets config bits for Shadow Register Set.

* '''1/20/2013''' [[Media:NU32_old_bootloader.zip|Nick's bootloader app as of Jan 20, that doesn't compile]]
* Reconfiguring the pins.


'''Simple PIC'''
'''Simple PIC'''
* '''TESTED 1/16/2013''' [[Media:simplePIC.c|simplePIC.c]]: Flashes LEDs on the NU32 unless the USER button is pressed.
* '''TESTED 1/16/2013''' [[Media:simplePIC_standalone.c|simplePIC_standalone.c]]: Standalone version of simplePIC.c.


* [[Media:simplePIC.c|simplePIC.c]]: Flashes LEDs on the NU32 unless the USER button is pressed.


'''NU32 library, header file, procdefs, and terminal comm test'''
'''NU32 library, header file, procdefs, and terminal comm test'''
* '''1/17/2013''' [[Media:talkingPIC.c|talkingPIC.c]]: echoes strings sent from keyboard back to computer screen

* '''1/21/2013''' [[Media:NU32bootloaded.ld|NU32bootloaded.ld]]: Use this linker script when creating executables to be bootloaded on NU32 (not app.ld). Kevin's version which puts the program starting at 0x9D000000, allowing access to full amount of flash.
* [[Media:NU32.c|NU32.c]]: NU32 board library.
* [[Media:NU32.h|NU32.h]]: NU32 header file.
* '''1/20/2013''' [[Media:NU32.c|NU32.c]]: NU32 board library.
* '''1/28/2013''' [[Media:NU32.h|NU32.h]]: NU32 header file, updated to have config bits for Shadow Register Set and to include <plib.h>.
* [[Media:procdefs.ld|procdefs.ld]]: Linker file for NU32.
* [[Media:NU32_serial_comm.c|NU32_serial_comm.c]]: Serial communication test with the terminal.
* [[Media:NU32_serial_comm.c|NU32_serial_comm.c]]: Serial communication test with the terminal.
* [[Media:NU32_serial_plot.c|NU32_serial_plot.c]]: Plot data with serial communication in NU32_Utility.
* [[Media:NU32_serial_plot.c|NU32_serial_plot.c]]: Plot data with serial communication in NU32_Utility.


'''LCD code (to be moved into NU32.c)'''
'''Interrupts'''
* [[Media:LCD.c|LCD.c]]: LCD library.
* [[Media:LCD.h|LCD.h]]: LCD header file.
* [[Media:LCDtest.c|LCDtest.c]]: Test program for LCD library.


'''Serial Sample code for Invest.c'''
* [[Media:INT_core_timer.c|INT_core_timer.c]]: Interrupt on core timer period match.
* [[Media:NU32_serial_for_invest.c|NU32_serial_for_invest.c]]: An example of serial communication, useful for converting invest.c to the PIC32.
* [[Media:INT_ext_int.c|INT_ext_int.c]]: Interrupt on rising edge of external interrupt line INT0.

* Peripheral timer interrupt.
'''Communication Examples'''
* Serial to a terminal
* Serial to MATLAB
* Serial to Processing
* Output to the LCD


'''Digital I/O'''
'''Digital I/O'''

* [[Media:DIGIO_sfrs.c|DIGIO_sfrs.c]]: Digital I/O and change notification using SFR manipulation.
* [[Media:DIGIO_sfrs.c|DIGIO_sfrs.c]]: Digital I/O and change notification using SFR manipulation.
* [[Media:DIGI0_plib.c|DIGIO_plib.c]]: Digital I/O and change notification using the peripheral library.
* [[Media:DIGI0_plib.c|DIGIO_plib.c]]: Digital I/O and change notification using the peripheral library.


'''Counter/timer'''
'''Counter/timer'''

* [[Media:TMR_16bit.c|TMR_16bit.c]]: Set up a 16-bit timer to count the PBCLK.
* [[Media:TMR_16bit.c|TMR_16bit.c]]: Set up a 16-bit timer to count the PBCLK.
* [[Media:TMR_16bit_external_clocks.c|TMR_16bit_external_clocks.c]]: Count two external clock sources, such as the A and B channel from a motor encoder.
* [[Media:TMR_16bit_external_clocks.c|TMR_16bit_external_clocks.c]]: Count two external clock sources, such as the A and B channel from a motor encoder.


'''Output compare (PWM)'''
'''Interrupts'''
* [[Media:INT_core_timer.c|INT_core_timer.c]]: Interrupt on core timer period match.
* [[Media:INT_ext_int.c|INT_ext_int.c]]: Interrupt on rising edge of external interrupt line INT0.
* Peripheral timer interrupt.


'''Output compare (PWM)'''
* [[Media:OC_PWM_nolib.c|OC_PWM_nolib.c]]: Set up PWM by setting SFRs.
* [[Media:OC_PWM_nolib.c|OC_PWM_nolib.c]]: Set up PWM by setting SFRs.
* [[Media:OC_PWM_lib.c|OC_PWM_lib.c]]: Set up PWM using peripheral library functions.
* [[Media:OC_PWM_lib.c|OC_PWM_lib.c]]: Set up PWM using peripheral library functions.
* [[Media:OC_square_wave.c|OC_square_wave.c]]: Alternate between two different PWM duty cycles at 1 Hz. PWM is at 100 kHz and can be low-pass filtered to get a square wave analog output.
* [[Media:OC_square_wave.c|OC_square_wave.c]]: Alternate between two different PWM duty cycles at 1 Hz. PWM is at 100 kHz and can be low-pass filtered to get a square wave analog output.


'''Using the UART''' (9/14)
'''Analog to digital'''
* [[Media:ADC_Read2.c|ADC_Read2.c]]: Reads two analog inputs repetitively and prints the results to the user's terminal.
* Simple RS232 communication: one UART to another on the same PIC
* [[Media:ADC_Read2_LCD.c|ADC_Read2_LCD.c]] Reads two analog inputs repetitively and prints the results to the LCD.
* Simple RS232 communication: PIC to PIC
* Something else, scanning mode, asynchronous results check on the buffer, and/or interrupt generated when some results are ready.
* RS232 with hardware flow control
* Talking to a PC with an FTDI cable


'''Using SPI''' (9/19)
'''Using the UART'''
* '''TESTED 2/6/2013''' [[Media:Uart_loop.c|Uart_loop.c]]: Simple RS232 communication: one UART to another on the same PIC
* SPI1 to SPI2 on one PIC
* '''TESTED 2/7/2013''' [[Media:Uart_pic2pic.c|Uart_pic2pic.c]]: Simple RS232 communication: PIC to PIC
* '''TESTED 2/7/2013''' [[Media:Uart_hfc.c|Uart_hfc.c]]: RS232 with hardware flow control
* '''TESTED 2/10/2013''' [[Media:Uart_ftdi.c|Uart_ftdi.c]]: Talking to a PC with an FTDI cable

'''Using I2C'''
* '''TESTED 2/20/2013''' [[Media:NU32_I2C.c|NU32_I2C.c]], [[Media:NU32_I2C.h|NU32_I2C.h]]: Library files.
* I2C1 to I2C2 on one PIC
* PIC to PIC
* '''TESTED 2/20/2013''' [[Media:I2C_DAC.c|I2C_DAC.c]]: MAX518 8bit I2C DAC
* '''TESTED 2/20/2013''' [[Media:I2C_ADC.c|I2C_ADC.c]]: NAU7802 24bit I2C ADC
* '''TESTED 2/20/2013''' [[Media:I2C_RAM.c|I2C_RAM.c]]: PCF8570 2kbit I2C SRAM

'''Using SPI'''
* '''TESTED 2/7/2013''' [[Media:SPILoopback.c|SPILoopback.c]]: SPI4 to SPI1 on one PIC
* PIC to PIC
* PIC to PIC
* MCP4922 12bit SPI DAC
* MCP4922 12bit SPI DAC
Line 66: Line 88:
* Microchip 23k256 256kbit SPI SRAM
* Microchip 23k256 256kbit SPI SRAM


'''USB''' (9/24)
'''Comparator'''
* '''TESTED 2/7/2013''' [[Media:Comparator.c‎|Comparator.c‎]]: Toggle an LED based on the relative state of two pins
* HID to Processing on a PC
* '''TESTED 2/7/2013''' [[Media:ComparatorOutput.c‎|ComparatorOutput.c‎]]: Output analogue ref voltage based on the difference between source pins
* CDC to PC

'''Using I2C''' (9/28)
* I2C1 to I2C2 on one PIC
* PIC to PIC
* MAX518 8bit I2C DAC
* NAU7802 24bit I2C ADC
* PCF8570 2kbit I2C SRAM

'''Comparator''' (10/5)
* turn a light on or off based on potentiometer reading
* turn a light on or off based on potentiometer reading
* 16-value analog output?


'''Input Capture''' (10/12)
'''Input Capture'''
* create a PWM signal (duty cycle controlled by a potentiometer) and read in to an input capture, display the pulse width or duty cycle
* '''TESTED 2/7/2013''' [[Media:Input_capture.c|Input_capture.c]]: Create a PWM signal (duty cycle controlled by a potentiometer) and read in to an input capture, display the pulse width or duty cycle

'''Watchdog Timer''' (10/16)
* Press a button every so often or watch the PIC reset itself (and perhaps a light stays off until you manually reset it or something)

'''Using the RTCC''' (10/19)
* Keeping time: set time from a PC, display current time on same PC
* alarm clock demo?

'''Programming the onboard flash''' (10/26)
* Flash self programming: displays saved data at power on, allows user to enter new data (button presses, whatever), displays the new data, and when you power cycle, PIC displays the most recent data on startup

'''Sleep, Idle, Freeze''' (11/2)
* config bits?
* demonstrate little current usage in sleep mode; wake up at predefined intervals or on certain signals and do something (and see the spike in current usage?)

'''Using CAN''' (11/9)
* Three PICs establishing a CAN bus: each PIC has a button and the state of the LEDs on each NU32 changes depending on which buttons are pressed (confirm communication between PICs)
* PC to PIC (Peak card?)
* some CAN chip? sensor?

'''Analog to digital''' (11/16)
* [[Media:ADC_Read2.c|ADC_Read2.c]]: Reads two analog inputs repetitively and prints the results to the user's terminal.
* [[Media:ADC_Read2_LCD.c|ADC_Read2_LCD.c]] Reads two analog inputs repetitively and prints the results to the LCD.
* Something else, scanning mode, asynchronous results check on the buffer, and/or interrupt generated when some results are ready.


== Applications ==
== Applications ==
Line 124: Line 112:
* [[Media:NU32_rcservo_digio_example.c|NU32_rcservo_digio_example.c]]: Use any digital IO pin and a timer to set the position of the servo
* [[Media:NU32_rcservo_digio_example.c|NU32_rcservo_digio_example.c]]: Use any digital IO pin and a timer to set the position of the servo



'''LCD code'''
== Extra PIC32 Code ==
* [[Media:LCD.c|LCD.c]]: LCD library.

* [[Media:LCD.h|LCD.h]]: LCD header file.
'''USB'''
* [[Media:LCDtest.c|LCDtest.c]]: Test program for LCD library.
* '''TESTED 2/17/2013''' [[Media:USBHost-MassStorage.zip|USBHost-MassStorage.zip]]: Writes the voltage on an analog pin to a ADC.csv file on an external USB stick approx. 5 times per second. Writing starts when the USB is inserted and stops when the USER button is pressed. Remember to press the USER button to stop writing data before removing the USB stick. View the README.txt file to build
* HID to Processing on a PC
* CDC to PC

'''Watchdog Timer'''
* '''TESTED 2/7/2013''' [[Media:WatchdogTimer.c|WatchdogTimer.c]]: User button turns on LED the LED turns off again when PIC restarts due to watchdog timer
* Press a button every so often or watch the PIC reset itself (and perhaps a light stays off until you manually reset it or something)

'''Using the RTCC'''
* Keeping time: set time from a PC, display current time on same PC
* alarm clock demo?

'''Programming the onboard flash'''
* Flash self programming: displays saved data at power on, allows user to enter new data (button presses, whatever), displays the new data, and when you power cycle, PIC displays the most recent data on startup

'''Sleep, Idle, Freeze'''
* config bits?
* demonstrate little current usage in sleep mode; wake up at predefined intervals or on certain signals and do something (and see the spike in current usage?)

'''Using CAN'''
* Three PICs establishing a CAN bus: each PIC has a button and the state of the LEDs on each NU32 changes depending on which buttons are pressed (confirm communication between PICs)
* PC to PIC (Peak card?)
* some CAN chip? sensor?

'''DIP PIC32 with reconfigurable pins (1/2xx)''' (11/21)

* Reconfiguring the pins.


'''Data logging on a uSD card'''
'''Data logging on a uSD card'''

Latest revision as of 06:50, 16 January 2016

OBSOLETE. THIS PAGE REFERS TO A PRE-RELEASE VERSION OF THE NU32 PIC32 DEVELOPMENT BOARD. FOR INFORMATION, SAMPLE CODE, AND VIDEOS RELATED TO THE PRODUCTION VERSION (2016 AND LATER), AND TO THE CORRESPONDING BOOK "EMBEDDED COMPUTING AND MECHATRONICS WITH THE PIC32 MICROCONTROLLER," VISIT THE NU32 PAGE.


Code for ME333 instructors to test before it goes live.

Code with TESTED has been tested and is ready to go live.

Code with EDITS has been updated but not fully test.

Code without any marking has not been looked at yet.


PIC32 Code

Bootloader Project

  • 1/30/2013 NU32_bootloader.zip: (Updated 1/30 to make it jump to the noncacheable virtual address, not cacheable, just for conceptual clarity.) All files in one folder. Kevin's version building on Nick's. Jumps to application at new jump location that doesn't waste flash. Also sets config bits for Shadow Register Set.
  • 1/20/2013 Nick's bootloader app as of Jan 20, that doesn't compile

Simple PIC

  • TESTED 1/16/2013 simplePIC.c: Flashes LEDs on the NU32 unless the USER button is pressed.
  • TESTED 1/16/2013 simplePIC_standalone.c: Standalone version of simplePIC.c.


NU32 library, header file, procdefs, and terminal comm test

  • 1/17/2013 talkingPIC.c: echoes strings sent from keyboard back to computer screen
  • 1/21/2013 NU32bootloaded.ld: Use this linker script when creating executables to be bootloaded on NU32 (not app.ld). Kevin's version which puts the program starting at 0x9D000000, allowing access to full amount of flash.
  • 1/20/2013 NU32.c: NU32 board library.
  • 1/28/2013 NU32.h: NU32 header file, updated to have config bits for Shadow Register Set and to include <plib.h>.
  • NU32_serial_comm.c: Serial communication test with the terminal.
  • NU32_serial_plot.c: Plot data with serial communication in NU32_Utility.

LCD code (to be moved into NU32.c)

Serial Sample code for Invest.c

Communication Examples

  • Serial to a terminal
  • Serial to MATLAB
  • Serial to Processing
  • Output to the LCD

Digital I/O

  • DIGIO_sfrs.c: Digital I/O and change notification using SFR manipulation.
  • DIGIO_plib.c: Digital I/O and change notification using the peripheral library.

Counter/timer

Interrupts

  • INT_core_timer.c: Interrupt on core timer period match.
  • INT_ext_int.c: Interrupt on rising edge of external interrupt line INT0.
  • Peripheral timer interrupt.

Output compare (PWM)

  • OC_PWM_nolib.c: Set up PWM by setting SFRs.
  • OC_PWM_lib.c: Set up PWM using peripheral library functions.
  • OC_square_wave.c: Alternate between two different PWM duty cycles at 1 Hz. PWM is at 100 kHz and can be low-pass filtered to get a square wave analog output.

Analog to digital

  • ADC_Read2.c: Reads two analog inputs repetitively and prints the results to the user's terminal.
  • ADC_Read2_LCD.c Reads two analog inputs repetitively and prints the results to the LCD.
  • Something else, scanning mode, asynchronous results check on the buffer, and/or interrupt generated when some results are ready.

Using the UART

  • TESTED 2/6/2013 Uart_loop.c: Simple RS232 communication: one UART to another on the same PIC
  • TESTED 2/7/2013 Uart_pic2pic.c: Simple RS232 communication: PIC to PIC
  • TESTED 2/7/2013 Uart_hfc.c: RS232 with hardware flow control
  • TESTED 2/10/2013 Uart_ftdi.c: Talking to a PC with an FTDI cable

Using I2C

  • TESTED 2/20/2013 NU32_I2C.c, NU32_I2C.h: Library files.
  • I2C1 to I2C2 on one PIC
  • PIC to PIC
  • TESTED 2/20/2013 I2C_DAC.c: MAX518 8bit I2C DAC
  • TESTED 2/20/2013 I2C_ADC.c: NAU7802 24bit I2C ADC
  • TESTED 2/20/2013 I2C_RAM.c: PCF8570 2kbit I2C SRAM

Using SPI

  • TESTED 2/7/2013 SPILoopback.c: SPI4 to SPI1 on one PIC
  • PIC to PIC
  • MCP4922 12bit SPI DAC
  • MCP3008 10bit SPI ADC
  • Microchip 23k256 256kbit SPI SRAM

Comparator

  • TESTED 2/7/2013 Comparator.c‎: Toggle an LED based on the relative state of two pins
  • TESTED 2/7/2013 ComparatorOutput.c‎: Output analogue ref voltage based on the difference between source pins
  • turn a light on or off based on potentiometer reading

Input Capture

  • TESTED 2/7/2013 Input_capture.c: Create a PWM signal (duty cycle controlled by a potentiometer) and read in to an input capture, display the pulse width or duty cycle

Applications

DSP and FFT

Reading a quadrature encoder with the dsPIC33FJ12MC201 breakout board

Controlling a stepper motor with the TB6612

Controlling an RC servo motor


Extra PIC32 Code

USB

  • TESTED 2/17/2013 USBHost-MassStorage.zip: Writes the voltage on an analog pin to a ADC.csv file on an external USB stick approx. 5 times per second. Writing starts when the USB is inserted and stops when the USER button is pressed. Remember to press the USER button to stop writing data before removing the USB stick. View the README.txt file to build
  • HID to Processing on a PC
  • CDC to PC

Watchdog Timer

  • TESTED 2/7/2013 WatchdogTimer.c: User button turns on LED the LED turns off again when PIC restarts due to watchdog timer
  • Press a button every so often or watch the PIC reset itself (and perhaps a light stays off until you manually reset it or something)

Using the RTCC

  • Keeping time: set time from a PC, display current time on same PC
  • alarm clock demo?

Programming the onboard flash

  • Flash self programming: displays saved data at power on, allows user to enter new data (button presses, whatever), displays the new data, and when you power cycle, PIC displays the most recent data on startup

Sleep, Idle, Freeze

  • config bits?
  • demonstrate little current usage in sleep mode; wake up at predefined intervals or on certain signals and do something (and see the spike in current usage?)

Using CAN

  • Three PICs establishing a CAN bus: each PIC has a button and the state of the LEDs on each NU32 changes depending on which buttons are pressed (confirm communication between PICs)
  • PC to PIC (Peak card?)
  • some CAN chip? sensor?

DIP PIC32 with reconfigurable pins (1/2xx) (11/21)

  • Reconfiguring the pins.

Data logging on a uSD card

  • Using the Microchip FAT32 library, AN1045