728x90

728x90

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.
 

No comments:

Post a Comment

320x50

Search This Blog

tyro-728x90