冰楓論壇

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

[討論] 製作電度機

[複製鏈接]

2609

主題

0

好友

945

積分

高級會員

Rank: 4

UID
373967
帖子
7392
主題
2609
精華
0
積分
945
楓幣
962
威望
925
存款
26000
贊助金額
0
推廣
0
GP
1205
閱讀權限
50
在線時間
405 小時
註冊時間
2023-1-12
最後登入
2024-4-29

2023端午節紀念勳章 2023中秋節紀念勳章 2023聖誕節紀念勳章

跳轉到指定樓層
1
發表於 2023-4-28 19:42:56 |只看該作者 |倒序瀏覽
需要以下零件:

PIC16F628微控制器
LCD顯示屏
3.電流互感器
4.電容
5.電阻
6.電感
7.電位器
8.二極體
9.電解電容
10.繼電器
11.鐵芯繼電器
12.光電耦合器
13.開關
14.連接線
15.麵包板

PIC16F628的連接腳位如下:

Pin        Name        Function
1        RB0        Input
2        RB1        Input/Output
3        RB2        Input/Output
4        RB3        Input/Output
5        RB4        Input/Output
6        RB5        Input/Output
7        RB6        Input/Output/PGM Data
8        RB7        Input/Output/PGM Clock
9        VSS        Ground
10        VDD        Power Supply (5V)
11        OSC1        Crystal Oscillator
12        OSC2        Crystal Oscillator
13        RC0        Input/Output
14        RC1        Input/Output
15        RC2        Input/Output
16        RC3        Input/Output
17        RC4        Input/Output
18        RC5        Input/Output/SSP Clock
19        RC6        Input/Output/USART TX/CK
20        RC7        Input/Output/USART RX/DT

這裡提供PIC16F628電度機程式碼
你可以根據自己的需要進行修改和調整。


#include <pic16f628.h>

// LCD模組的定義
#define LCD_RS      RB1
#define LCD_EN      RB2
#define LCD_DATA    PORTC

// 電流互感器的定義
#define CT          RB0

// 電度計的變量
float voltage = 0;          // 電壓
float current = 0;          // 電流
float power = 0;            // 功率
float energy = 0;           // 電能
float energy_total = 0;     // 總電能

// LCD模組初始化
void lcd_init()
{
    // 設定輸出模式
    TRISB1 = 0;
    TRISB2 = 0;
    TRISC = 0;

    // 初始化LCD
    LCD_RS = 0;
    LCD_EN = 0;
    LCD_DATA = 0x38;
    __delay_ms(15);
    LCD_EN = 1;
    __delay_ms(5);
    LCD_EN = 0;
    __delay_ms(5);

LCD_DATA = 0x38;
__delay_ms(5);
LCD_EN = 1;
__delay_ms(5);
LCD_EN = 0;
__delay_ms(5);

LCD_DATA = 0x0C;
__delay_ms(5);
LCD_EN = 1;
__delay_ms(5);
LCD_EN = 0;
__delay_ms(5);

LCD_DATA = 0x01;
__delay_ms(5);
LCD_EN = 1;
__delay_ms(5);
LCD_EN = 0;
__delay_ms(5);

LCD_DATA = 0x06;
__delay_ms(5);
LCD_EN = 1;
__delay_ms(5);
LCD_EN = 0;
__delay_ms(5);
}

// LCD模組寫入字元
void lcd_write_char(unsigned char c)
{
LCD_RS = 1;
LCD_EN = 0;
LCD_DATA = c;
LCD_EN = 1;
__delay_us(40);
LCD_EN = 0;
__delay_us(40);
}

// LCD模組寫入命令
void lcd_write_cmd(unsigned char c)
{
LCD_RS = 0;
LCD_EN = 0;
LCD_DATA = c;
LCD_EN = 1;
__delay_us(40);
LCD_EN = 0;
__delay_us(40);
}

// LCD模組顯示字符串
void lcd_write_string(unsigned char *str)
{
while(*str)
{
lcd_write_char(*str++);
}
}

// 讀取ADC值
unsigned int adc_read(unsigned char channel)
{
ADCON0 = ((channel << 2) & 0b00111100) | (ADCON0 & 0b11000011);
__delay_us(20);
GO_nDONE = 1;
while(GO_nDONE);
return (ADRESH << 8) | ADRESL;
}

// 初始化ADC
void adc_init()
{
ADCON0 = 0b00000001;
ADCON1 = 0b00001110;
ADRESH = 0;
ADRESL = 0;
}

// 主函數
void main()
{
// 初始化
lcd_init();
adc_init();

// 主循環
while(1)
{
    // 讀取電壓
    unsigned int voltage_adc = adc_read(0);
    voltage = voltage_adc * 5.0 / 1024.0;

    // 讀取電流
    unsigned int current_adc = adc_read(1);
    current = current_adc * 5.0 / 1024.0;

    // 計算功率
    power = voltage * current;

    // 計算電能
    energy += power / 3600.0;

    // 計算總電能
    energy_total += energy;

    // 顯示數據
    lcd_write_cmd(0x80);
    lcd_write_string("Voltage: ");
    lcd_write_string(voltage);
    lcd_write_string("V");
    lcd_write_cmd(0xC0);
    lcd_write_string("Current: ");
    lcd_write_string(current);
    lcd_write_string("A");
    lcd_write_cmd(0x94);
    lcd_write_string("Power: ");
    lcd_write_string(power
    lcd_write_string("W");
    lcd_write_cmd(0xD4);
    lcd_write_string("Energy: ");
    lcd_write_string(energy);
    lcd_write_string("Wh");
    lcd_write_cmd(0x87);
    lcd_write_string("Total: ");
    lcd_write_string(energy_total);
    lcd_write_string("Wh");

    // 延遲一秒
    __delay_ms(1000);
}
}

這是電度機程式,使用了PIC16F628微控制器、LCD模組和ADC模組。

需要的零件清單如下:

PIC16F628微控制器
16x2字符LCD模組
10k歐姆電阻器
0.1uF陶瓷電容器
1uF電解電容器
LM35溫度感測器
直流電源

連接腳位如下:

LCD_RS -> PIC16F628 RA2腳
LCD_EN -> PIC16F628 RA3腳
LCD_DATA -> PIC16F628 PORTB腳
ADC模組 -> PIC16F628 PORTA腳

程式碼中有一些基本的函數,例如lcd_init()用於初始化LCD模組,lcd_write_char()和lcd_write_cmd()用於向LCD模組寫入字符和命令,adc_init()用於初始化ADC模組,adc_read()用於讀取ADC值。

在主函數中,首先初始化LCD模組和ADC模組。
然後進入主循環,在循環中讀取電壓和電流值,計算功率和電能,並顯示在LCD模組上。
最後延遲一秒後進入下一輪循環。
收藏收藏0 推0 噓0


把本文推薦給朋友或其他網站上,每次被點擊增加您在本站積分: 1骰子

相關文章

複製連結並發給好友,以賺取推廣點數
簡單兩步驟,註冊、分享網址,即可獲得獎勵! 一起推廣文章換商品、賺$$
高級模式
B Color Image Link Quote Code Smilies |上傳

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

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

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

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

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

GMT+8, 2024-4-29 00:35

回頂部