PIC Microcontroller

PIC16F15355 PWM control Source file

EP 기술연구소 2018. 12. 10. 05:04

전원을 on 하였을때 4Octave '도' 부터 14개의 음이 상승하고 전원이 OFF 될때 14개의 음이 하강한다.

PWM output 사용시 Timer2는 비활성화 되어 있어야 한다.

one chip Microcontroller Program 작성시 가능한 한글은 사용하지 않는것이 좋다.

가끔 오류가 발생하는 원인이 되기도 한다. 필요시 복사해서 그대로 사용하면 된다.


<F15355.h>  : header file


#device ADC=16

#FUSES NOWDT                      //Watch Dog Timer
#FUSES NOEXTOSC                 //External Oscillator not enabled
#FUSES NOBROWNOUT               //No brownout reset
#FUSES NOLVP                    //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES PROTECT                  //Code protected from reads

#use delay(internal=16000000)
#USE PWM(OUTPUT=PIN_C2, FREQUENCY=1kHz, DUTY=50)
//#pin_select CCP1OUT=PIN_C2
//#pin_select PWM5OUT=PIN_A2 //PWM3


#nolist
//=======================================================================================
#define   Allbits      8
#define   Bit0      0
#define   Bit1      1
#define   Bit2      2
#define   Bit3      3
#define   Bit4      4
#define   Bit5      5
#define   Bit6      6
#define   Bit7      7

// VDD = 20,  VSS = 8, 19

#Byte   PORTA    = 0x0C
#bit   ADC0      = PORTA.Bit0   // Pin = 2 (AN0 = Temporature ADC Input)
#bit   Tilt_Sen   = PORTA.Bit1   // Pin = 3 (AN1 = Tilt switch input)
#bit   TestPin      = PORTA.Bit2   // Pin = 4 (not used & TestPin)
#bit   Segment_e   = PORTA.Bit3   // Pin = 5 (FND Segment_e)
#bit   KeyInput0   = PORTA.Bit4   // Pin = 6 (Key input_0 port)
#bit   KeyInput1   = PORTA.Bit5   // Pin = 7 (Key input_1 port)
#bit   KeyInput2   = PORTA.Bit6   // Pin = 10 (Key input_2 port)
#bit   LampLED_0   = PORTA.Bit7   // Pin = 9 (LampLED common 0 = Power, Time)

#Byte   PORTB    = 0x0D      // Key input port
#bit   Ext_Int0   = PORTB.Bit0   // Pin = 21 (External Interrupt 0 )
#bit   Segment_b   = PORTB.Bit1   // Pin = 22 (FND Segment_b)
#bit   Segment_c   = PORTB.Bit2   // Pin = 23 (FND Segment_c)
#bit   Segment_d   = PORTB.Bit3   // Pin = 24 (FND Segment_d)
#bit   IR_Input   = PORTB.Bit4   // Pin = 25 (Imfrared Remocon Input)
#bit   Segment_f   = PORTB.Bit5   // Pin = 26 (FND Segment_f)
#bit   Segment_g   = PORTB.Bit6   // Pin = 27 (FND Segment_g)
#bit   Segment_dot   = PORTB.Bit7   // Pin = 28 (FND Segment_dot)

#Byte   PORTC    = 0x0E
#bit   Segment_a   = PORTC.Bit0   // Pin = 11 (FND Segment_a)
#bit   LampLED_1   = PORTC.Bit1   // Pin = 12 (LampLED common 0 = Lower, Strong)
#bit   BuzOut      = PORTC.Bit2   // Pin = 13 (Buzzer Out)
#bit   Triac1_Out   = PORTC.Bit3   // Pin = 14 (Triac Control = Strong_1)
#bit   Triac2_Out   = PORTC.Bit4   // Pin = 15 (Triac Control = Strong_2)
#bit   Triac3_Out   = PORTC.Bit5   // Pin = 16 (Triac Control = Brower FAN )_
#bit   FND_Com0   = PORTC.Bit6   // Pin = 17 (FND Common_0)
#bit   FND_Com1   = PORTC.Bit7   // Pin = 18 (FND Common_1)

#Byte   PORTD    = 0x0F      // Used of LCD Module Data-port

#Byte   PORTE    = 0x10
//#bit         = PORTE.bit3    // Pin = 1(MCLR)


//#Byte   LATA   = 0x018
//#Byte   LATB    = 0x019
//#Byte   LATC    = 0x01A
//#Byte   ANSELA   = 0x1F38
//#byte ANSELA=getenv("SFR:ANSELA")

long ADRES;
#Byte   ADRES   = 0x9B
#Byte   ADRESL   = 0x9B
#Byte   ADRESH   = 0x9C

#byte ADCON0=getenv("SFR:ADCON0")
//#Byte   ADCON0   = 0x9D
#Byte   ADCON1   = 0x9E
#Byte   ADCON2   = 0x9F

/*
long TMR1;
#Byte   TMR1   = 0x20C         
#Byte   TMR1L   = 0x20C
#Byte   TMR1H   = 0x20D

#Byte   TMR2   = 0x28C

*/
#byte   RC2PPS   = 0x1F22

#byte   CCPR1L  = 0x30C
#byte   CCPR1H  = 0x30D
#byte   CCP1CON = 0x30E
#byte   CCP1CAP = 0x30F


//=======================================================================================
//// Immediate Data Define      /////
/////////////////////////////////////

#define   on   1
#define   Off   0

#define   PWM_OUTPUT   0x20
//=======================================================================================
//// RAM Define      /////
/////////////////////////

short    mSec1Flag;

//=====Buzzer control define
short BuzFlag, BuzOnFlag;
unsigned char BuzTimer, BuzCount;

#define   melody_key    48 //59
#define   start_melody 36 //48   // 5 octave, do
#define   end_melody      (start_melody + 12) //59
short   MelodyFlag , MelodyUp;
unsigned char   MolodyKey;

//==============================================================================
// 7-Segement data define = h,g,f,e,d,c,b,a (Common Cathode Type)
const char Seg7_Tbl[] = {0x7D, 0x28, 0x5B, 0x7A, 0x2E, 0x76, 0x77, 0x3C,
                     0x7F, 0x7E, 0x3F, 0x67, 0x43, 0x6B, 0x57, 0x17,
                     0x0  };

//
const unsigned long  Melody_tbl[] = {
   31,     33,   35,   37,   41,   44,   46,   49,   52,   55,   58,   62,      // Octave 0
   65,       69,   73,   78,   82,   87,   93,   98,  104,  110,  117,  123,      // Octave 1
   131,   139,  147,  156,  165,  175,  185,  196,  208,  220,  233,  247,      // Octave 2
   262,   277,  294,  311,  330,  349,  370,  392,  415,  440,  466,  494,      // Octave 3
   523,   554,  587,  622,  659,  698,  740,  784,  831,  880,  932,  988,      // Octave 4
   1047, 1109, 1175, 1245, 1319, 1397, 1480, 1568, 1661, 1760, 1865, 1976,      // Octave 5
   2093, 2217, 2349, 2489, 2637, 2794, 2960, 3136, 3322, 3520, 3729, 3951,      // Octave 6
   4186, 4435, 4699, 4978, 5274, 5589, 5920, 6272, 6645, 7040, 7459, 7902       // Octave 7
   };
  
//==============================================================================

#list
//#end

<F15355.c>  : main file


#include <stddef.h>
#include <stdio.h>
#ZERO_RAM

#include "InfraHeaterCtl_sub.c"
#include "ADC_control.c"
#include "KeyCheck.c"
#include "FND_Display.c"

#include "HEFlash_Ctl.c"  // Internal High-Endurance Flash Data Memory (HEF)

//================================================================
#INT_EXT
void  EXT_isr(void)
{
 if (--Sec1Counter == 0) { // 1Sec check routine
        Sec1Counter = 60;
        Sec1Flag = 1;
    }
}

#INT_TIMER0
void  TIMER0_isr(void)
{
 //setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1|RTCC_8_BIT);  //64.0 us overflow

}

#INT_AD
void  AD_isr(void)

}

#INT_TIMER1
void  TIMER1_isr(void)
{
 //setup_timer_1(T1_INTERNAL|T1_DIV_BY_4);  //65.5 ms overflow
 set_timer1(0xFFFF - 1000);  // 1mSec timer Interrupt
 mSec1Flag = 1;
}

#INT_TIMER2
void  TIMER2_isr(void)
{
 
}

//===============================================================================
void main()
{
 setup_wdt(WDT_1MS);  //~1.0 ms reset

  //IO Port Initial (0=output, 1=input)
    PORTA = 0b10000101;    //ANSELA = 1;//0xFE;
 set_tris_a(0b01110011);
    PORTB = 0x10;
    set_tris_b(0b00010001);
    //port_b_pullups(0x10);
    PORTC = 0x04;
    set_tris_c(0b00000000); // All output
    PORTE = 0xFF;
    set_tris_e(0);
    //port_e_pullups(0xFF);
 
 setup_adc_ports(sAN0, VSS_VDD);
 setup_adc(ADC_CLOCK_INTERNAL);//setup_adc(ADC_CLOCK_DIV_8);
 ADCON1 |= (1 << ADFM);
 
 setup_timer_0(RTCC_INTERNAL|RTCC_DIV_2|RTCC_8_BIT);  //64.0 us overflow
 setup_timer_1(T1_INTERNAL|T1_DIV_BY_4);  //65.5 ms overflow
 //setup_timer_2(T2_DIV_BY_1,24,1);   //100 us overflow, 100 us interrupt
 
 enable_interrupts(INT_EXT);
 enable_interrupts(INT_TIMER0);
 enable_interrupts(INT_AD);
 enable_interrupts(INT_TIMER1);
 //enable_interrupts(INT_TIMER2);
 
 //setup_ccp1 (CCP_PWM | PWM_ENABLED | PWM_OUTPUT | PWM_ACTIVE_HIGH);
 setup_ccp1(CCP_PWM);
 set_pwm1_duty((int16)248);
 
 DataInitial();
 enable_interrupts(GLOBAL);
   
    while(TRUE)
 {
  restart_wdt(); // watchdog timer clear
  
  if (mSec1Flag) {
            mSec1Flag = 0;
          
            /* // Buzzer control */
            if (BuzFlag) BuzTimeCtl();
            if (BuzOnFlag) BuzOut = 1;
            else BuzOut = 0;
   
            TimerCheck();   // Timer increment
            ADC_Start();   // ADC Start
   KeyScan();    // Key check Routine
   TiltSensorChk(); // Tilt Sensor check
   
         // FND Display Rouyjtine
         if(TempSenErrFlag | TiltSenErrFlag){
    PowerOnFlag = 0;
    FND_ErrorDisplay();  // 7-Segment Error display routine
   }
   else FND_Display();   // 7-Segment display routine
  
  /* FND_TestDisplay();*/ 

  //TestPin = ~TestPin;
  //TODO: User Code
 }
}