冰楓論壇

 找回密碼
 立即註冊
ads_sugarbook
搜索
查看: 622|回覆: 0
打印 上一主題 下一主題

[討論] PIC16F1786

[複製鏈接]
跳轉到指定樓層
1
匿名  發表於 2023-4-26 05:41:48 |倒序瀏覽 | 取消回覆通知
製作變頻器所需的零件包括:

PIC16F1786 微控制器
16x2 字元 LCD 顯示器
10K 溫度補償可變電阻
10K 固定電阻
1uF 陶瓷電容
22pF 陶瓷電容
4MHz 晶振
NPN 三極管
雙向二極體

以下是 PIC16F1786 的連接腳位:

RA0 - 用作 PWM 輸出
RA4 - 用作 LCD 顯示器的 RS 引腳
RA5 - 用作 LCD 顯示器的 EN 引腳
RC0 - 用作 PWM 的計數器輸入
RC1 - 用作 PWM 的計數器輸入
RC2 - 用作三角波發生器的輸出
RC5 - 用作 LCD 顯示器的 D4 引腳
RC6 - 用作 LCD 顯示器的 D5 引腳
RC7 - 用作 LCD 顯示器的 D6 引腳
RD0 - 用作 LCD 顯示器的 D7 引腳
RD4 - 用作溫度補償可變電阻的引腳
RD5 - 用作雙向二極體的引腳
RD6 - 用作 NPN 三極管的引腳

以下是變頻器的程式碼:


#include <xc.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "lcd.h"

#pragma config FOSC=INTOSC
#pragma config WDTE=OFF
#pragma config PWRTE=OFF
#pragma config MCLRE=ON
#pragma config CP=OFF
#pragma config CPD=OFF
#pragma config BOREN=ON
#pragma config CLKOUTEN=OFF
#pragma config IESO=OFF
#pragma config FCMEN=OFF

#define _XTAL_FREQ 4000000
#define PWM_PERIOD 1000
#define PI 3.14159265358979323846

unsigned char sine_table[] = {128, 153, 178, 202, 225, 246, 255, 254, 241, 218, 193, 168, 144, 121, 100, 80, 62, 47, 34, 24, 16, 11, 7, 5, 5, 7, 11, 16, 24, 34, 47, 62, 80, 100, 121, 144, 168, 193, 218, 241, 254, 255, 246, 225, 202, 178, 153, 128, 102, 77, 53, 30, 9, 0, 1,unsigned char get_sine_value(unsigned char index) {
if (index >= 50) {
index = 99 - index;
}
return sine_table[index];
}

void main() {
OSCCONbits.IRCF = 0b110; // set oscillator frequency to 4 MHz
OSCCONbits.SCS = 0b10; // use internal oscillator

TRISA = 0b00000000; // set port A as output
TRISC = 0b00000000; // set port C as output
TRISD = 0b00010000; // set port D as output except for RD4 (temperature compensation variable resistor)

ANSELA = 0b00000000; // set port A as digital
ANSELC = 0b00000000; // set port C as digital
ANSELD = 0b00000000; // set port D as digital

T2CONbits.T2CKPS = 0b11; // set timer 2 prescaler to 1:64
T2CONbits.TMR2ON = 1; // enable timer 2
PR2 = 124; // set timer 2 period to 125 cycles (4 us)

CCP1CONbits.CCP1M = 0b1100; // set CCP1 as PWM mode
CCP1CONbits.DC1B = 0; // set PWM duty cycle bits to 0
CCPR1L = 0; // set PWM duty cycle to 0

LCD_Init(); // initialize LCD
LCD_Clear(); // clear LCD screen

unsigned char index = 0;
while (1) {
    unsigned char sine_value = get_sine_value(index); // get sine value from lookup table
    CCPR1L = sine_value >> 1; // set PWM duty cycle to half of sine value
    CCP1CONbits.DC1B = sine_value & 0b00000001; // set PWM duty cycle LSB according to sine value

    char frequency_string[17];
    float frequency = (sine_value + 128) * 50.0 / 256.0; // calculate frequency from sine value
    sprintf(frequency_string, "Frequency: %.2f Hz", frequency); // format frequency as string
    LCD_SetCursor(0, 0); // set LCD cursor to first row, first column
    LCD_Print(frequency_string); // print frequency to LCD screen

    __delay_ms(10); // wait for 10 ms
    index++; // increment index to cycle through lookup table
    if (index == 100) {
        index = 0; // reset index to 0 after reaching end of lookup table
    }
}
}



這段程式碼使用 PWM 控制 PIC16F1786 的 RA0 腳位輸出變頻信號,同時使用 LCD 顯示器顯示當前的頻率。在程式的一開始,設置了 PIC16F1786 的 OSCCON 和 T2CON 註冊表,並將 CCP1 設置為 PWM 模式。

然後,程式使用 sine_table 查找表來生成變頻信號。

在主循環中,程式從查找表中獲取 sine 值,將其轉換為頻率,並顯示在 LCD

收藏收藏0 推0 噓0


把本文推薦給朋友或其他網站上,每次被點擊增加您在本站積分: 1骰子
複製連結並發給好友,以賺取推廣點數
簡單兩步驟,註冊、分享網址,即可獲得獎勵! 一起推廣文章換商品、賺$$
高級模式
B Color Image Link Quote Code Smilies |上傳

廣告刊登意見回饋關於我們職位招聘本站規範DMCA隱私權政策

Copyright © 2011-2024 冰楓論壇, All rights reserved

免責聲明:本網站是以即時上載留言的方式運作,本站對所有留言的真實性、完整性及立場等,不負任何法律責任。

而一切留言之言論只代表留言者個人意見,並非本網站之立場,用戶不應信賴內容,並應自行判斷內容之真實性。

小黑屋|手機版|冰楓論壇

GMT+8, 2024-5-4 02:55

回頂部