PIC16F877 outputs sine wave signal PROTEUS simulation and program

Publisher:JoyfulSunflowerLatest update time:2019-10-28 Source: 51heiKeywords:PIC16F877 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

//The PIC16F877 microcontroller outputs a sine wave signal simulation schematic diagram as follows


//20MHz crystal oscillator, outputs 500Hz sine wave, 100 points per cycle.

//1 cycle time is 2ms, the interval between two points is 2000/100=20us

//Use TMR2 to delay, 256*Tcy*K=20, Tcy=0.2, take K=1

//PR2 calculation: (PR2+1)*Tcy*1=20, PR2=99


#include

__CONFIG(0x3F3A);

#define        CS                RB1

#define        LDAC        RB2


char BUF,N;

#define  nn   100

const unsigned int SS[100]={

2048,2111,2173,2235,2297,2357,2416,2474,2530,2584,2636,2685,2733,2777,2819,2857,

2892,2924,2953,2978,2999,3017,3030,3040,3046,3048,3046,3040,3030,3017,2999,2978,

2953,2924,2892,2857,2819,2777,2733,2685,2636,2584,2530,2474,2416,2357,2297,2235,

2173,2111,2048,1985,1923,1861,1799,1739,1680,1622,1566,1512,1460,1411,1363,1319,

1277,1239,1204,1172,1143,1118,1097,1079,1066,1056,1050,1048,1050,1056,1066,1079,

1097,1118,1143,1172,1204,1239,1277,1319,1363,1411,1460,1512,1566,1622,1680,1739,

1799,1861,1923,1985};


//Macro definition, the final read of SSPBUF is a must!

#define SPI_WRITE(A)       

        SSPBUF=A;                       

        while(STAT_BF==0);       

        BUF=SSPBUF


void        main(void)

{        unsigned int x;


        TRISB=0b11111001;

        CS=1;NOP();

        LDAC=1;

        TRISC=0b00010000; //RC3 output (SCK), RC4 input (SDI), RC5 output (SDO)

        SSPEN=1; //SPI serial port enable

        CKP=1; //Idle clock is high level

        SSPM3=0;                

        SSPM2=0;

        SSPM1=0;

        SSPM0=0; //SPI master mode, clock is Fosc/4

        STAT_SMP = 1; // Sample input data at the end of the data output time

        STAT_CKE=0; //Transmit data on the rising edge of SCK

        PR2=99; //According to calculation, interrupt once every 20us

        T2CON=0b00000100;        

        N=0;

        TMR2IE=1; //Enable TMR2 interrupt

        LIKE=1;

        GIE=1;

        while(1);

}


void        interrupt INT_ISR(void)

{        unsigned        int X;

        char        i,j;

        if (TMR2IF==1)

        {        TMR2IF=0;

                X=SS[N++];

                if (N>=nn)

                        N=0;

                X+=0x7000; //HSDN=1, reference voltage buffer, amplification factor GA=1

                CS=0; //Chip select is valid

                i=X>>8;

                j=X;

                SPI_WRITE(i); //output the high 8 bits of the command

                SPI_WRITE(j); //output the lower 8 bits of the command

                LDAC=0;NOP(); //DA output is valid

                LDAC=1;NOP();

                CS=1;

        }

}



Keywords:PIC16F877 Reference address:PIC16F877 outputs sine wave signal PROTEUS simulation and program

Previous article:PIC16F684 MCU Timer Interrupt Marquee
Next article:PIC microcontroller electronic clock program + simulation circuit

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号