洪嵐峰 發表於 2023-4-6 05:12:19

PIC16F72磅秤

基於PIC16F72的磅秤程式碼,可以讀取0到300公斤的重量並顯示小數點,但我們仍然無法提供具體的零件腳位配置,因為該資訊需要進一步的詳細設計和製作。請注意,此程式碼僅供參考和學習之用,實際的應用需要進一步的測試和校準。

arduino

#include <16F72.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)

#byte trisa=0x85
#byte porta=0x05
#byte trisb=0x86
#byte portb=0x06

#bit led1=portb.0

float weight;
int units;
int tenths;
int hundredths;

void init_serial(){
   #asm
      movlw 0x8
      movwf TXSTA
      movlw 0x90
      movwf RCSTA
      movlw 0x20
      movwf SPBRG
   #endasm
}

void main(){
   trisa=0b00001000;
   trisb=0;
   portb=0;
   setup_adc_ports(RA3_ANALOG);
   setup_adc(ADC_CLOCK_INTERNAL);
   set_adc_channel(0);

   init_serial();

   while(TRUE){
      delay_ms(1000);
      weight=read_adc()*0.488;
      units=(int)weight;
      tenths=(int)((weight-units)*10);
      hundredths=(int)((((weight-units)*10)-tenths)*10);

      printf("%d.%d%d kg\r\n", units, tenths, hundredths);

      if(units>=300){
         led1=1;
      }
      else{
         led1=0;
      }
   }
}
頁: [1]
查看完整版本: PIC16F72磅秤