// // ProCoach program for dsPIC33FJ32MC202 // #include <libpic30.h> #include <p33fj32mc202.h> #include <stdio.h> #include <math.h> // Configuration settings _FOSC(CSW_FSCM_OFF & FRC_PLL16); // Fosc=16x7.5MHz, Fcy=30MHz _FWDT(WDT_OFF); // Watchdog timer off _FBORPOR(MCLR_DIS); // Disable reset pin // Function prototypes void setup(); unsigned int read_analog_channel(int n); int main() { // Set up digital i/o, analog input, PWM, UART and interrupt setup(); // Flash LEDs on RD0 and RD1 at 1Hz for 4 seconds while (1) { _LATD0 = 0; __delay32(15000000); _LATD0 = 1; __delay32(15000000); } return 0; } // This function sets up digital i/o, analog input, UART void setup() { // Configure all four port D pins (RD0, RD1, RD2, RD3) // as digital outputs LATD = 0; TRISD = 0b1111111111110000; // Configure all three port C pins (RC13, RC14, RC15) // as digital inputs TRISC = 0b1111111111111111; // Configure AN0-AN8 as analog inputs TRISB = 0x01FF; // All 9 port B pins are inputs ADPCFG = 0xFE00; // Lowest 9 PORTB pins are analog inputs ADCON1 = 0; // Manually clear SAMP to end sampling, start conversion ADCON2 = 0; // Voltage reference from AVDD and AVSS ADCON3 = 0x0005; // Manual Sample, ADCS=5 -> Tad = 3*Tcy ADCON1bits.ADON = 1; // Turn ADC ON // Set up UART // Default is 8 data bits, 1 stop bit, no parity bit U1BRG = 48; // 38400 baud @ 30 MIPS U1MODEbits.UARTEN = 1; // Enable UART U1STAbits.UTXISEL = 1; // interrupt when TX buffer is empty U1STAbits.UTXEN = 1; // Enable TX } // This function reads a single sample from the specified // analog input. It should take less than 2.5us if the chip // is running at about 30 MIPS. // Because the dsPIC30F4011 has a 10-bit ADC, the value // returned will be between 0 and 1023. unsigned int read_analog_channel(int channel) { ADCHS = channel; // Select the requested channel ADCON1bits.SAMP = 1; // Start sampling __delay32(30); // 1us delay @ 30 MIPS ADCON1bits.SAMP = 0; // Start Converting while (!ADCON1bits.DONE); // Should take 12 * Tad = 1.2us return ADCBUF0; }
Copyright © 2010-2019 Ted Burke, All Rights Reserved.
Blog Stats
- 1,434,239 hits
- My Tweets
-
Recent Posts
- Moving statue of David Foster Wallace
- TU Dubuntu 19.10 – a customised live USB version of Xubuntu Linux 19.10 for TU Dublin
- Using Canon EOS 70D DSLR as a USB web cam in Xubuntu Linux 19.04
- TU Dubuntu – a customised live USB version of Xubuntu Linux 19.04 for TU Dublin
- Using the PHP command line web server to transfer files between devices on a local network
- €5 PPG – photoplethysmogram amplifier / Arduino circuit
- Clap detector circuit / AirSpell typing system
- AirMouse – control mouse pointer in Linux using one switch or by blowing on microphone
- Some RGB fractal doodles
- How to display USB webcam as live video on desktop using mplayer
- Ronan Byrne’s ultra low-cost brain-computer interface
- H-bridge control example for Arduino Nano (ATmega328) – two phase-displaced square waves
- An offcut from the Fraktalismus pattern factory
- Cafe Terrace at Starry Night
- €2 Robots in DIT
- A brief introduction to binary numbers…
- Can the PIC12F675 drive motors directly from its GPIO pins?
- Minimum Viable Program for PIC12LF1572
- dsPIC30F Quadrature Encoder Interface (QEI) – Basic Example
- Simple example program for the PIC12F675 microcontroller
- A simple way to read and write audio and video files in C using FFmpeg (part 2: video)
- A simple way to read and write audio and video files in C using FFmpeg (part 1: audio)
- Fraktalismus Outtakes: Sea Monsters
- Fraktalismus – my presentation at Dublin Maker 2016
- Example code from presentation: Ways of Seeing Julia Sets
- C or Python? Comparison of execution time for Mandelbrot image generation
- Julia Set Explorer
- (no title)
- What I’m working on right now…
- Simple 2-channel hardware PWM example for the MSP430G2452 microcontroller
- RoboSlam @ Dublin Maker – only two days away!
- Generating antiphase PWM signals with the dsPIC30F4011
- Very simple Python / Tkinter GUI to send selected keystrokes via serial port
- Using a dsPIC30F4011 to generating 4 PWM signals with equal duty cycles but at 90 degree phase increments
- Faster Mandelbrot image generation using numpy in Python
- Fractal variations using Python
- Using SendInput to type unicode characters
- Three PWM outputs with three different frequencies using the dsPIC30F4011 microcontroller
- 8-channel PWM with the MSP430G2553
- Simple Phaser Framework example – Flappy Words
- Installing Microchip XC16 in CrunchBang Linux
- Low-cost motor control by displaying coloured shapes on a phone or PC screen
- Phasor diagrams in GNU Octave
- 2D Room Mapping With a Laser and a Webcam
- Motion Tracking on the Cheap with a PIC
- Big Foot Magic Hands
- Keyboard shortcut to simulate a mouse right-click in CrunchBang Linux
- Real-time analysis of data from BioSemi ActiveTwo via TCP/IP using Python
- Programming the PIC16F819 on an LCD03 adapter using a PICkit 2
- Simple communication with a TCP/IP device using Python
Recent Comments
Maram Alanhhas on Clap detector circuit / AirSpe… Orit Malki on A simple way to read and write… Orit Malki on A simple way to read and write… Tim-Felix on Running CommandCam from Excel… Benjah on Simple command line trick for… Archives
- November 2019 (2)
- July 2019 (1)
- June 2019 (1)
- May 2019 (1)
- April 2019 (3)
- January 2019 (1)
- September 2018 (1)
- May 2018 (1)
- April 2018 (1)
- February 2018 (1)
- January 2018 (1)
- May 2017 (1)
- April 2017 (4)
- February 2017 (3)
- August 2016 (1)
- July 2016 (1)
- February 2016 (2)
- January 2016 (3)
- November 2015 (1)
- July 2015 (1)
- April 2015 (1)
- March 2015 (2)
- November 2014 (1)
- October 2014 (2)
- September 2014 (1)
- April 2014 (1)
- March 2014 (2)
- February 2014 (4)
- January 2014 (5)
- December 2013 (2)
- November 2013 (3)
- October 2013 (1)
- September 2013 (1)
- June 2013 (2)
- May 2013 (1)
- April 2013 (4)
- March 2013 (4)
- February 2013 (5)
- January 2013 (5)
- December 2012 (6)
- November 2012 (7)
- October 2012 (7)
- September 2012 (2)
- August 2012 (8)
- July 2012 (1)
- June 2012 (2)
- May 2012 (4)
- April 2012 (7)
- February 2012 (5)
- January 2012 (1)
- December 2011 (2)
- November 2011 (11)
- October 2011 (3)
- August 2011 (1)
- July 2011 (1)
- May 2011 (1)
- April 2011 (4)
- December 2010 (4)
- November 2010 (2)