728x90

728x90

Saturday, September 27, 2025

dsPIC30F2010 and ds18B20 Temperature Interfacing

The DS18B20 is a digital thermometer that able to convert the temperature from -55 to +125 degree Celsius. The controller communicates with this temperature sensor using a 1-Wire bus. So this device requires only three wires including GND, VCC, and data line. Furthermore it can use data line to power  the device eliminating the VCC. 

dsPIC30F2010 and ds18B20 Temperature Interfacing

 

This device also work in TTL logic level that could interface with the dsPIC30F2010 using its digital bi-directional I/O. 

PIC16F84A DS18B20 1-Wire Temperature Reading And Multiplexing Display Example Using XC8
DS18B20 Pin Configuration
PIC16F84A DS18B20 1-Wire Temperature Reading And Multiplexing Display Example Using XC8
DS18B20 In TO-92 Package

For more information and interfacing this sensor with a micro-controller please check this post. 

The newer version of CCS PICC (v5.119) have a C driver "ds18b20.c"for this device that allow the programmer to call it using a few lines of code. It's effective and fast.

The following example I use RB0 of dsPIC30F2010 to read the temperature sensor from a single wire ds18b20 temperature sensor. The sensor's data will be send over the RS-232 between the micro-controller and the host PC. I also added a TC1604A-04 16x4 LCD to display the temperature data in degree Celsius and Fahrenheit.  

  • CCS PICC Source Code (v5.119) 

 


  1. /*CCS PICC Compiler version 5.119*/
  2. #include <30F2010.h>
  3. #fuses HS,NODEBUG,NOWDT,PR,CKSFSM
  4. #use delay(clock=20M)
  5. #use rs232(UART1,BAUD=9600)


  6. #define PIN_DS18B20_DATA PIN_B0
  7. #include "ds18b20.c"

  8. #define LCD_RS_PIN PIN_E4
  9. #define LCD_RW_PIN PIN_E5
  10. #define LCD_ENABLE_PIN PIN_E5
  11. #define LCD_DATA4 PIN_E0
  12. #define LCD_DATA5 PIN_E1
  13. #define LCD_DATA6 PIN_E2
  14. #define LCD_DATA7 PIN_E3

  15. #include "lcd.c"

  16. #define lcd_clear() lcd_putc('\f')
  17. #define lcd_home() lcd_putc('\a')
  18. /*For 16x4 LCD Only*/
  19. #define line_1() lcd_send_byte(0,0x80);
  20. #define line_2() lcd_send_byte(0,0xC0);
  21. #define line_3() lcd_send_byte(0,0x90);
  22. #define line_4() lcd_send_byte(0,0xD0);



  23. void main(){
  24. signed int16 val;
  25. printf("\n\rdsPIC30F2010 Prototype Board.");
  26. printf("\n\rSaturday 26th September 2025");
  27. printf("\n\rds18b20 Humidity Sensor Example\n\r");
  28. printf("\r\n\r\ds18b20.c - DHT11 example starting\r\n\r\n");
  29. ds18b20_init();
  30. lcd_init();
  31. printf(LCD_PUTC,"dsPIC30F2010 LCD");
  32. line_2();
  33. printf(LCD_PUTC,"ds18b20 Sensor");
  34. line_3();
  35. printf(LCD_PUTC,"Programming");
  36. line_4();
  37. printf(LCD_PUTC,"CCS PICC v5.049");
  38. delay_ms(5000);
  39. lcd_clear();
  40. while(1){
  41. ds18b20_read(&val);
  42. printf("temperature = %ldC\r\n", val/(signed int16)16);
  43. lcd_home();
  44. printf(LCD_PUTC," ds18b20 Sensor");
  45. line_2();
  46. printf(LCD_PUTC," Temperature");
  47. line_3();
  48. printf(LCD_PUTC," %ld%cC",val/(signed int16)16,0xDF);
  49. line_4();
  50. printf(LCD_PUTC," and %f%cF",1.8*(val/(signed int16)16)+32,0xDF);
  51. delay_ms(1000);
  52. }
  53. }

 I modified the "lcd.c" C driver file to make it works with my TC1604A-04 LCM module. So I need to copy this driver file to my project folder. The "ds18b20.c" C driver file is already existed in the driver folder of CCS PICC v5.119. But I also copied to my project folder.

This program needs 51% of ROM and 36% of RAM.

dsPIC30F2010 and ds18B20 Temperature Interfacing
Serial Input/Output Monitor

I use my DIY dsPIC30F2010 Prototype Board offered by PCBWAY to test this programming example.

dsPIC30F2010 and ds18B20 Temperature Interfacing
Start Up Program

dsPIC30F2010 and ds18B20 Temperature Interfacing
ds18B20 Temperature Reading


Click here to download this example.

 

Friday, September 26, 2025

dsPIC30F2010 and DHT-11 Environmental Sensor Interfacing

The DHT11 temperature and humidity sensor features a temperature and humidity sensor
complex with a calibrated digital signal output. By using the exclusive digital-signal-acquisition
technique and temperature and humidity sensing technology, it ensures high reliability and
excellent long-term stability. This sensor includes a resistive-type humidity measurement
component and an NTC temperature measurement component, and connects to a high performance 8-bit micro-controller, offering excellent quality, fast response, anti-interference
ability and cost-effectiveness.

dsPIC30F2010 and DHT-11 Environmental Sensor Interfacing

Its standard TTL bi-directional I/O suitable for dsPIC30F2010 Interfacing and Programming. It needs only three wires, +5VDC, DATA and GND. Its DATA pin requires a weak pull up resistor with a resistance around 4.7k. 

I use my dsPIC30F2010 DIY Prototype Board to test this program. 

PIC16F84A DHT11 Temperature And Humidity Sensor And Character LCD Interfacing Using XC8
DHT11 Humidity & Temperature
Sensor

Parameters

Relative Humidity
Resolution:             16Bit
Repeatability:         ±1%RH
Accuracy:               25℃ ±5%RH
Interchangeability:  Fully interchangeable
Response time:       1/e (63%)25℃ 6s
                                1m/s Air 6s
Hysteresis:             <±0.3%RH
Long-term stability: <±0.5%RH/yr


Temperature
Resolution:             16Bit
Repeatability:         ±1℃
Accuracy:               25℃ ±2℃
Response time:       1/e (63%) 10S


Electrical Characteristics
Power supply:         DC 3.3~ 5.5V
Supply current:       Measure 0.3mA Standby 60μA
Sampling period:     Secondary Greater than 2 seconds 

For more detail about interfacing this sensor with a micro-controller please see this post. 

dsPIC30F2010 CCS PICC Programming

The newer version of CCS PICC (v5.119) has a C driver for the DHT-11 environmental sensor that could be called using a few lines of code. The "dht11.c" C driver locates in the drivers directory in program files. I use pin RB0 of dsPIC30F2010 to interface with this sensor.

The following example shows a simple sensor reading and data displaying over the RS-232.

  • board.h 
  1. #include <30F2010.h>
  2. #device ICSP=1
  3. #fuses HS,NODEBUG,NOWDT,PR,CKSFSM
  4. #use delay(crystal=20000000)

  5. #define LED0 PIN_D0
  6. #define LED1 PIN_D1
  7. #define SW0 PIN_C13
  8. #define SW1 PIN_C14

  9. #define DELAY 500

  10. #use rs232(UART1, baud=9600, stream=UART_PORT1)

 

  •  main.c
  1. #include "board.h"

  2. #define PIN_DHT11_DATA PIN_B0
  3. #include <dht11.c>

  4. void main(){
  5. unsigned int8 relativeHumidity;
  6. unsigned int8 tempC;
  7. printf("\n\rdsPIC30F2010 Prototype Board.");
  8. printf("\n\rWednesday 25 September 2025");
  9. printf("\n\rDHT-11 Humidity Sensor Example\n\r");
  10. printf("\r\n\r\dht-11.c - DHT11 example starting\r\n\r\n");
  11. dht11_init();
  12. delay_ms(1000);
  13. while(1){
  14. dht11_read(&relativeHumidity, &tempC);
  15. printf("HUMIDITY=%03u%%, TEMPERATURE=%02uC\r\n", relativeHumidity, tempC);
  16. delay_ms(2000);
  17. }
  18. }

 

  • Memory Use 


  •  Serial Input/Output Monitor

 


 
 Click here to download this example.

I add a 16x4 LCD (TC1604A-04) to display temperature and humidity data. Using a character LCD is very conventional for most small micro-controller programming and interfacing. I copied the "lcd.c" 16x2 LCD C driver to my project folder that I can modify it without changing the original file. I disable the LCD R/W pin (wire to GND) and I upgrade this LCD driver to a 16x4 LCD.

Its "board.h" header file remains the same to above example. 

  • main.c

 

  1. #include "board.h"

  2. #define PIN_DHT11_DATA PIN_B0
  3. #include <dht11.c>

  4. #define LCD_RS_PIN PIN_E4
  5. #define LCD_RW_PIN PIN_E5
  6. #define LCD_ENABLE_PIN PIN_E5
  7. #define LCD_DATA4 PIN_E0
  8. #define LCD_DATA5 PIN_E1
  9. #define LCD_DATA6 PIN_E2
  10. #define LCD_DATA7 PIN_E3

  11. #include "lcd.c"

  12. #define lcd_clear() lcd_putc('\f')
  13. #define lcd_home() lcd_putc('\a')
  14. /*For 16x4 LCD Only*/
  15. #define line_1() lcd_send_byte(0,0x80);
  16. #define line_2() lcd_send_byte(0,0xC0);
  17. #define line_3() lcd_send_byte(0,0x90);
  18. #define line_4() lcd_send_byte(0,0xD0);


  19. void main(){
  20. unsigned int8 relativeHumidity;
  21. unsigned int8 tempC;
  22. printf("\n\rdsPIC30F2010 Prototype Board.");
  23. printf("\n\rFriday 26 September 2025");
  24. printf("\n\rDHT-11 Humidity Sensor Example\n\r");
  25. printf("\r\n\r\dht-11.c - DHT11 example starting\r\n\r\n");
  26. dht11_init();
  27. lcd_init();
  28. printf(LCD_PUTC,"dsPIC30F2010 LCD");
  29. line_2();
  30. printf(LCD_PUTC,"DHT-11 Sensor");
  31. line_3();
  32. printf(LCD_PUTC,"Example Using");
  33. line_4();
  34. printf(LCD_PUTC,"CCS PICC v5.119");
  35. delay_ms(5000);
  36. lcd_clear();
  37. while(1){
  38. dht11_read(&relativeHumidity, &tempC);
  39. printf("HUMIDITY=%03u%%, TEMPERATURE=%02uC\r\n", relativeHumidity, tempC);
  40. lcd_home();
  41. printf(LCD_PUTC," DHT-11 Sensor");
  42. line_2();
  43. printf(LCD_PUTC," Reading:");
  44. line_3();
  45. printf(LCD_PUTC,"Humidity: %03u%%",relativeHumidity);
  46. line_4();
  47. printf(LCD_PUTC,"Temperature:%02u%cC",tempC,0xDF);
  48. delay_ms(1000);
  49. }
  50. }

 The TC1604A-04 works fine even it's too old.

dsPIC30F2010 and DHT-11 Environmental Sensor Interfacing
Start-Up Screen


dsPIC30F2010 and DHT-11 Environmental Sensor Interfacing
DHT-11 Sensor Reading and Displaying

Click here to download this example.
 


Tuesday, September 16, 2025

dsPIC30F2010 Prototype Board CCS PICC Examples (Continue)

In previous post I putted some examples of using the dsPIC30F2010 prototype board. However it's too long. So I need to write some remaining posts here.

dsPIC30F2010 Prototype Board CCS PICC Examples (Continue)
PCBWay.com Sponsor PCB Project

 

Creating a PWM Output Using Code Generation Wizard

Generating a PWM output signal could be done from scratch with a few line of code using CCS PICC. We can use its code generation wizard or even manually. 

dsPIC30F2010 Prototype Board CCS PICC Examples (Continue)
PWM Output Pin of OC1
 

After clicking on Create Project it will generate source code. Then pressing F9 to compile this project.

The main.c C source code is just like below.

  1. #include <main.h>


  2. void main()
  3. {

  4. while(TRUE)
  5. {
  6. //TODO: User Code
  7. }

  8. }

 Then double click on the main.h header file we will see its source code.

  1. #include <30F2010.h>
  2. #device ICSP=1
  3. #use delay(crystal=20000000)

  4. #FUSES NOWDT //No Watch Dog Timer
  5. #FUSES CKSFSM //Clock Switching is enabled, fail Safe clock monitor is enabled


  6. #use pwm(OC1,TIMER=2,FREQUENCY=10000,DUTY=0)


It will generate a PWM signal output at pin OC1 (RC13) with a frequency of 10kHz and 0% duty cycle. If we want a 50% duty cycle we need to change the DUTY parameter to 50, and rebuilt it.

PWM Duty Cycle Adjusting with ADC

After using the code generation wizard I got some idea of using PWM in CCS PICC. So I modify and write more codes to adjust PWM signal. I use the on-board ADC input from a potentiometer. Then it will convert to PWM duty cycle ranging from 0% to 100%.

  1. #include "board.h"

  2. #use pwm(OC1,TIMER=2,FREQUENCY=10000,STREAM=_1,DUTY=50)
  3. void main()
  4. {
  5. long adc_value = 0;
  6. float duty_cycle = 0;
  7. setup_adc_ports(sAN4);
  8. setup_adc(ADC_CLOCK_INTERNAL | ADC_TAD_MUL_31);


  9. while(TRUE)
  10. {
  11. //TODO: User Code
  12. set_adc_channel(4);
  13. delay_us(10);
  14. adc_value = read_adc();
  15. duty_cycle = (1000.0*adc_value)/1023;
  16. pwm_set_duty(_1,(int)duty_cycle);
  17. //pwm_set_duty_percent(_1,(int)duty_cycle);
  18. delay_ms(100);
  19. }

  20. }


And its "board.h" file:

  1. #include <30F2010.h>
  2. #device ADC=10
  3. #device ICSP=1
  4. #fuses HS,NODEBUG,NOWDT,PR,CKSFSM
  5. #use delay(crystal=20000000)


  6. #use FIXED_IO( D_outputs=PIN_D1,PIN_D0 )
  7. #use rs232(UART1, baud=9600, stream=UART_PORT1)

  8. #define LED0 PIN_D0
  9. #define LED1 PIN_D1
  10. #define SW0 PIN_C13
  11. #define SW1 PIN_C14

  12. #define DELAY 500


It work fine without wiring additional components. However we can connect the OC1(RC13) PWM pin to a larger LED ( for instance a 5VDC 10mm LED).

 

dsPIC30F2010 Prototype Board CCS PICC Examples (Continue)
Low Duty Cycle

dsPIC30F2010 Prototype Board CCS PICC Examples (Continue)
High Duty Cycle

Click here to download its source file.  

ADC with On-board Tactile Switches and PWM

Fortunately there are two on-board tactile switches that could be used to adjust PWM duty cycle. So I will use ADC channel 4, SW4 (RC13) and SW5(RC14) to adjust PWM duty cycle of OC1 and OC2 respectively.

dsPIC30F2010 Prototype Board CCS PICC Examples (Continue)
PWM Adjustment Using Pot and Tactile Switches

The PWM OC1 is generated by Timer 2 while the PWM OC2 is generated by Timer 3. 

  1. #include "board.h"

  2. #use pwm(OC1,TIMER=2,FREQUENCY=10000,STREAM=_1,DUTY=0)
  3. #use pwm(OC2,TIMER=3,FREQUENCY=10000,STREAM=_2,DUTY=0)

  4. void main()
  5. {
  6. long adc_value = 0;
  7. int oc2_count=0;
  8. float duty_cycle = 0;
  9. set_pullup(TRUE,PIN_C13);
  10. set_pullup(TRUE,PIN_C14);
  11. setup_adc_ports(sAN4);
  12. setup_adc(ADC_CLOCK_INTERNAL | ADC_TAD_MUL_31);

  13. while(TRUE)
  14. {
  15. //TODO: User Code
  16. set_adc_channel(4);
  17. delay_us(10);
  18. adc_value = read_adc();
  19. duty_cycle = (1000.0*adc_value)/1023;
  20. pwm_set_duty(_1,(int)duty_cycle);
  21. if(input(SW0)==0){
  22. if(oc2_count<1000) oc2_count+=100;
  23. pwm_set_duty(_2,oc2_count);
  24. delay_ms(250);
  25. }
  26. if(input(SW1)==0){
  27. if(oc2_count>0) oc2_count-=100;
  28. pwm_set_duty(_2,oc2_count);
  29. delay_ms(250);
  30. }
  31. }

  32. }


Its "board.h" header file:

  1. #include <30F2010.h>
  2. #device ADC=10
  3. #device ICSP=1
  4. #fuses HS,NODEBUG,NOWDT,PR,CKSFSM
  5. #use delay(crystal=20000000)

  6. #use rs232(UART1, baud=9600, stream=UART_PORT1)

  7. #define LED0 PIN_D0
  8. #define LED1 PIN_D1
  9. #define SW0 PIN_C13
  10. #define SW1 PIN_C14

  11. #define DELAY 500

Click here to download its source file.

Timer Tick Example

We can use timer tick for timing delay and scheduling instead of using the delay function. We can call it from scratch using the CCS PICC IDE PIC24 project wizard.

dsPIC30F2010 Prototype Board CCS PICC Examples (Continue)
Timer Tick Example Using Timer 1

 Then we get generated code lists below.

  • main.h
  1. #include <30F2010.h>
  2. #device ICSP=1
  3. #use delay(crystal=20000000)

  4. #FUSES NOWDT //No Watch Dog Timer
  5. #FUSES CKSFSM //Clock Switching is enabled, fail Safe clock monitor is enabled


  6. #use timer(timer=1,tick=100us,bits=32,NOISR)

  7. #define TICK_TYPE unsigned int32

  • main.c

 

  1. #include <main.h>
  2. TICK_TYPE GetTickDifference(TICK_TYPE currTick, TICK_TYPE prevTick)
  3. {
  4. return(currTick-prevTick);
  5. }

  6. void timer_1_tick(void)
  7. {
  8. //TODO: User Code
  9. }


  10. void main()
  11. {

  12. TICK_TYPE CurrentTick,PreviousTick;



  13. //Example program using Tick Timer
  14. CurrentTick = PreviousTick = get_ticks();

  15. while(TRUE)
  16. {
  17. CurrentTick = get_ticks();

  18. if(GetTickDifference(CurrentTick, PreviousTick) >= ((TICK_TYPE)TICKS_PER_SECOND*1)/1000)
  19. {
  20. timer_1_tick();
  21. PreviousTick = CurrentTick;
  22. }

  23. //TODO: User Code
  24. }

  25. }

 Then I need to add some codes to these existing source codes.

  •  main.h

 

  1. #include <30F2010.h>
  2. #device ICSP=1
  3. #use delay(crystal=20000000)

  4. #FUSES NOWDT //No Watch Dog Timer
  5. #FUSES CKSFSM //Clock Switching is enabled, fail Safe clock monitor is enabled


  6. #use FIXED_IO( D_outputs=PIN_D1,PIN_D0 )

  7. #define LED1 PIN_D0
  8. #define LED2 PIN_D1


  9. #use timer(timer=1,tick=100us,bits=32,NOISR)

  10. #define TICK_TYPE unsigned int32


  • main.c
  1. #include <main.h>

  2. unsigned int16 count_1_ms=0, count_100_ms=0;

  3. TICK_TYPE GetTickDifference(TICK_TYPE currTick, TICK_TYPE prevTick)
  4. {
  5. return(currTick-prevTick);
  6. }

  7. void timer_1_tick(void)
  8. {
  9. //TODO: User Code
  10. count_1_ms++;
  11. if(count_1_ms>=100) {
  12. output_toggle(LED2);
  13. count_1_ms=0;
  14. count_100_ms++;
  15. }
  16. if(count_100_ms>=5){
  17. output_toggle(LED1);
  18. count_100_ms=0;
  19. }
  20. }


  21. void main()
  22. {

  23. TICK_TYPE CurrentTick,PreviousTick;



  24. //Example program using Tick Timer
  25. CurrentTick = PreviousTick = get_ticks();

  26. while(TRUE)
  27. {
  28. CurrentTick = get_ticks();

  29. if(GetTickDifference(CurrentTick, PreviousTick) >= ((TICK_TYPE)TICKS_PER_SECOND*1)/1000)
  30. {
  31. timer_1_tick();
  32. PreviousTick = CurrentTick;
  33. }

  34. //TODO: User Code
  35. }

  36. }

 It will blink LEDs connect to RD0 and RD1 at different rates.

 

dsPIC30F2010 Prototype Board CCS PICC Examples (Continue)
RD0 and RD1 at different rates

dsPIC30F2010 Prototype Board CCS PICC Examples (Continue)
RD0 and RD1 at different rates

 Click here to download its source file.

Timer 1 Interrupt Example Using Coder Generation Wizard

Timer1 module operates in many modes up to software configuration, internal, gated and external. We can write its source manually or even using the CCS PICC code generation wizard. 

dsPIC30F2010 Prototype Board CCS PICC Examples (Continue)
Using Code Generation Wizard

Then it will generate a skeleton code below that we have to add more code manually.

  1. #include <main.h>

  2. #INT_TIMER1
  3. void timer1_isr(void)
  4. {

  5. }



  6. void main()
  7. {

  8. setup_timer1(TMR_INTERNAL | TMR_DIV_BY_1, 1000);

  9. enable_interrupts(INT_TIMER1);
  10. enable_interrupts(INTR_GLOBAL);

  11. while(TRUE)
  12. {
  13. //TODO: User Code
  14. }

  15. }

 Then I need to add more codes both in main function and Timer interrupt service routine "#INT_TIMER1".

 

  1. #include <main.h>

  2. unsigned int16 timer_1_counts=0;
  3. #INT_TIMER1
  4. void timer1_isr(void)
  5. {
  6. output_toggle(PIN_D0);
  7. timer_1_counts++;
  8. clear_interrupt(INT_TIMER1);
  9. }



  10. void main()
  11. {

  12. setup_timer1(TMR_INTERNAL | TMR_DIV_BY_256, 1000);

  13. enable_interrupts(INT_TIMER1);
  14. enable_interrupts(INTR_GLOBAL);

  15. while(TRUE)
  16. {
  17. //TODO: User Code
  18. if(timer_1_counts>=100){
  19. output_toggle(pin_d1);
  20. timer_1_counts=0;
  21. }
  22. }

  23. }

This source codes will blink pin RD0 and RD1 at different rates. Click here to download its source file.

Change Notify (CN) Interrupt

Change Notify Interrupt (CNI) allow the program to fast response to external event change. For instance a change from logic high to low or vice versa. There are two tactile switches connect to RC13(CN1) and RC14(CN0) respectively. There are no external pull up or pull down resistors. So you need to add more external components to detect input logic change requirement.

dsPIC30F2010 Prototype Board CCS PICC Examples (Continue)
TABLE 8-2: INPUT CHANGE NOTIFICATION REGISTER MAP (BITS 15-0)

Fortunately the Change Notify Interrupt (CNI) come with internal pull-up enable feature that we can enable or disable it by software setting. The CNENx and CNPUx special register responsible for this task. 

dsPIC30F2010 Prototype Board CCS PICC Examples (Continue) 

The program below demonstrate a simple use of Change Notify (CN) Interrupt of pin CN0(RC14). The LED connects to pin RD0 keeps blinking at the rate of 500ms in main program's loop. Whenever the CN0 input logic changes to logic low the CNI occurs. It will toggle the LED connects to pin RD1.

  • main.c 
  1. #include <main.h>

  2. /*CN Pull Up Enable Register*/
  3. #byte CNPU1=0x0C4;
  4. #bit CN0PU=CNPU1.0;
  5. /*CN Interrupt Enable Register*/
  6. #byte CNIEN1=0x0C0;
  7. #bit CN0EN=CNIEN1.0;

  8. /*Interrupt Service Routine for CN Interrupt*/
  9. #INT_CNI
  10. void cni_isr(void)
  11. {
  12. if(!input(CN0)) output_toggle(LED2);
  13. clear_interrupt(INT_CNI);
  14. }


  15. void main()
  16. {
  17. /*PortC As Inputs*/
  18. set_tris_c(0xFFFF);
  19. /*Turn On CN0 PullUp and CN0 Interrupt*/
  20. CN0PU=1;
  21. CN0EN=1;
  22. /*Enable Interrupt*/
  23. enable_interrupts(INTR_CN_PIN|PIN_C13);
  24. enable_interrupts(INTR_GLOBAL);
  25. clear_interrupt(INT_CNI);
  26. while(TRUE)
  27. {
  28. //TODO: User Code
  29. output_toggle(LED1);
  30. delay_ms(500);
  31. }

  32. }
  •  main.h
  1. #include <30F2010.h>
  2. #device ICSP=1
  3. #use delay(crystal=20000000)

  4. #FUSES NOWDT
  5. //No Watch Dog Timer
  6. #FUSES CKSFSM
  7. //Clock Switching is enabled, fail Safe clock monitor is enabled


  8. #use FIXED_IO( D_outputs=PIN_D1,PIN_D0 )

  9. #define CN1 PIN_C13
  10. #define CN0 PIN_C14
  11. #define LED1 PIN_D0
  12. #define LED2 PIN_D1



 I tested this demo program on my dsPIC30F2010 prototype board. It work fine and very fast. Click here to download this example program.

Now I use all tactile switches connect to RC13 and RC14 to generate Change Notification Interrupt (CNI). Every time the CN interrupts occur it will toggle the LEDs.

  • main.c

 

  1. #include <main.h>

  2. /*CN Pull Up Enable Register*/
  3. #byte CNPU1=0x0C4;
  4. #bit CN0PU=CNPU1.0;
  5. #bit CN1PU=CNPU1.1;
  6. /*CN Interrupt Enable Register*/
  7. #byte CNIEN1=0x0C0;
  8. #bit CN0EN=CNIEN1.0;
  9. #bit CN1EN=CNIEN1.1;

  10. /*Interrupt Service Routine for CN Interrupt*/
  11. #INT_CNI
  12. void cni_isr(void)
  13. {
  14. if(!input(CN0)) output_toggle(LED2);
  15. if(!input(CN1)) output_toggle(LED1);
  16. clear_interrupt(INT_CNI);
  17. }


  18. void main()
  19. {
  20. /*PortC As Inputs*/
  21. set_tris_c(0xFFFF);
  22. /*Turn On CN0 PullUp and CN0 and CN1 Interrupt*/
  23. CN0PU=1;
  24. CN0EN=1;
  25. CN1PU=1;
  26. CN1EN=1;
  27. /*Enable Interrupt*/
  28. //enable_interrupts(INTR_CN_PIN|PIN_C13);
  29. //enable_interrupts(INTR_CN_PIN|PIN_C14);
  30. enable_interrupts(INT_CNI);
  31. enable_interrupts(INTR_GLOBAL);
  32. clear_interrupt(INT_CNI);
  33. while(TRUE)
  34. {
  35. //TODO: User Code
  36. }

  37. }

I take some special registers of CN interrupt to use in this program because using the CCS PICC built-in library is not enough.

  • main.h
  1. #include <30F2010.h>
  2. #device ICSP=1
  3. #use delay(crystal=20000000)

  4. #FUSES NOWDT
  5. //No Watch Dog Timer
  6. #FUSES CKSFSM
  7. //Clock Switching is enabled, fail Safe clock monitor is enabled


  8. #use FIXED_IO( D_outputs=PIN_D1,PIN_D0 )

  9. #define CN1 PIN_C13
  10. #define CN0 PIN_C14
  11. #define LED1 PIN_D0
  12. #define LED2 PIN_D1




There are some bounce while pressing the tactile switches. So it's suitable to add and RC filter circuit to eliminate this noise. Using a software delay in the ISR is a good choice to bypass this noisy bouncing period.

dsPIC30F2010 Prototype Board CCS PICC Examples (Continue)

dsPIC30F2010 Prototype Board CCS PICC Examples (Continue)

dsPIC30F2010 Prototype Board CCS PICC Examples (Continue)

dsPIC30F2010 Prototype Board CCS PICC Examples (Continue)
 

Click here to download this program example.
 

320x50

Search This Blog

tyro-728x90