Could you help me about my project? I dose real time voltage monitor ADC by PIC16F877 and send the data value to PC (serial port). It’ seems a big problem about the sampling rate? I’m comparing my project between boards (ETT16F877) with Oscilloscope but result error very much. Need you help me for suggestions? Below picture my soft code PIC and VB6.(Sorry I'm not good english )
CCS for PIC.
#include <16F877.H>
#define TxD PIN_C6
#define RxD PIN_C7
#define CLOCK_SP 10000000
#fuses HS
#fuses NOLVP,NOWDT
#fuses NOPROTECT
#device ADC=10
#use delay (clock=CLOCK_SP)
#use rs232(baud=9600,xmit=TxD,rcv=RxD)
#use fast_io(A)
#define Vbe 0.0048875855327468230694037145650049 // Vref/1023 (5/1023)
void main(void) {
int16 value;
float volt;
setup_adc_ports(0x82); // A0 A1 A2 A3 A5 E0 E1 E2 Ref=Vdd
setup_adc(ADC_CLOCK_INTERNAL); // Clock RC
set_adc_channel(0); // Read Analog input RA0
do{
value = Read_ADC();
delay_us(10);
volt = Vbe*(float) value;
printf("\n\r%f",volt);
} while (TRUE);
}
Code VB6
Option Explicit
Dim data As String
Dim i As Variant
Dim n As Variant
Dim MyData As String
Dim stime As Variant
Dim sdate As String
Private Sub cmdExit_Click()
MSComm1.PortOpen = False
End
End Sub
Private Sub Command1_Click()
MSComm1.PortOpen = False
End Sub
Private Sub Form_Load()
Dim oExcel As Object
Dim oBook As Object
Dim oSheet As Object
Label4.Caption = " Data Save"
Shape2.BackColor = vbGreen
Shape2.BackStyle = 1
MSComm1.CommPort = 1
MSComm1.InBufferSize = 1024
MSComm1.OutBufferSize = 512
MSComm1.RThreshold = 1
MSComm1.Settings = "9600,n,8,1"
MSComm1.PortOpen = True
Timer3.Interval = 1000
Label6.Caption = Date
Label5.Caption = Time
If MSComm1.PortOpen = False Then
Shape1.BackColor = vbRed
Label2.Caption = " Disconnect to HW "
End If
MSChart1.ColumnCount = 1
MSChart1.RowCount = 30
MSChart1.chartType = VtChChartType2dLine
MSChart1.Plot.Axis(VtChAxisIdY).ValueScale.Auto = False
MSChart1.Plot.Axis(VtChAxisIdY).ValueScale.Minimum = 0
MSChart1.Plot.Axis(VtChAxisIdY).ValueScale.Maximum = 10
MSChart1.Plot.Axis(VtChAxisIdY).ValueScale.MajorDivision = 10
MSChart1.data = data
Timer1.Enabled = True
Timer2.Enabled = False
End Sub
Private Sub Label3_Change()
Open "C:\Voltage Detect " & Date$ & " .txt " For Append As #1
'Print #1, Date$ + Time$ + Label3.Caption
'Print #1, Trim$(Date$) + Trim$(Time$) + Trim$(Label3.Caption$)
Print #1, Date$ + " " + Time$ + " " + Label3.Caption$
Close #1
End Sub
Private Sub MSComm1_OnComm()
On Error Resume Next
If MSComm1.CommEvent = comEvReceive Then
data = MSComm1.Input
Label3.Caption = data
Shape1.BackColor = vbGreen
End If
MSChart1.data = data
For i = 1 To MSChart1.RowCount + 1
MSChart1.Row = i + 1
n = MSChart1.data
MSChart1.Row = i
MSChart1.data = n
Next i
MSChart1.Column = 1
MSChart1.Row = MSChart1.RowCount
End Sub
Private Sub Timer1_Timer()
Shape2.BackColor = vbRed
Shape2.BackStyle = 1
Timer2.Enabled = True
Timer1.Enabled = False
End Sub
Private Sub Timer2_Timer()
Shape2.BackColor = vbBlue
Shape2.BackStyle = 1
Timer2.Enabled = True
Timer1.Enabled = False
End Sub
CCS for PIC.
#include <16F877.H>
#define TxD PIN_C6
#define RxD PIN_C7
#define CLOCK_SP 10000000
#fuses HS
#fuses NOLVP,NOWDT
#fuses NOPROTECT
#device ADC=10
#use delay (clock=CLOCK_SP)
#use rs232(baud=9600,xmit=TxD,rcv=RxD)
#use fast_io(A)
#define Vbe 0.0048875855327468230694037145650049 // Vref/1023 (5/1023)
void main(void) {
int16 value;
float volt;
setup_adc_ports(0x82); // A0 A1 A2 A3 A5 E0 E1 E2 Ref=Vdd
setup_adc(ADC_CLOCK_INTERNAL); // Clock RC
set_adc_channel(0); // Read Analog input RA0
do{
value = Read_ADC();
delay_us(10);
volt = Vbe*(float) value;
printf("\n\r%f",volt);
} while (TRUE);
}
Code VB6
Option Explicit
Dim data As String
Dim i As Variant
Dim n As Variant
Dim MyData As String
Dim stime As Variant
Dim sdate As String
Private Sub cmdExit_Click()
MSComm1.PortOpen = False
End
End Sub
Private Sub Command1_Click()
MSComm1.PortOpen = False
End Sub
Private Sub Form_Load()
Dim oExcel As Object
Dim oBook As Object
Dim oSheet As Object
Label4.Caption = " Data Save"
Shape2.BackColor = vbGreen
Shape2.BackStyle = 1
MSComm1.CommPort = 1
MSComm1.InBufferSize = 1024
MSComm1.OutBufferSize = 512
MSComm1.RThreshold = 1
MSComm1.Settings = "9600,n,8,1"
MSComm1.PortOpen = True
Timer3.Interval = 1000
Label6.Caption = Date
Label5.Caption = Time
If MSComm1.PortOpen = False Then
Shape1.BackColor = vbRed
Label2.Caption = " Disconnect to HW "
End If
MSChart1.ColumnCount = 1
MSChart1.RowCount = 30
MSChart1.chartType = VtChChartType2dLine
MSChart1.Plot.Axis(VtChAxisIdY).ValueScale.Auto = False
MSChart1.Plot.Axis(VtChAxisIdY).ValueScale.Minimum = 0
MSChart1.Plot.Axis(VtChAxisIdY).ValueScale.Maximum = 10
MSChart1.Plot.Axis(VtChAxisIdY).ValueScale.MajorDivision = 10
MSChart1.data = data
Timer1.Enabled = True
Timer2.Enabled = False
End Sub
Private Sub Label3_Change()
Open "C:\Voltage Detect " & Date$ & " .txt " For Append As #1
'Print #1, Date$ + Time$ + Label3.Caption
'Print #1, Trim$(Date$) + Trim$(Time$) + Trim$(Label3.Caption$)
Print #1, Date$ + " " + Time$ + " " + Label3.Caption$
Close #1
End Sub
Private Sub MSComm1_OnComm()
On Error Resume Next
If MSComm1.CommEvent = comEvReceive Then
data = MSComm1.Input
Label3.Caption = data
Shape1.BackColor = vbGreen
End If
MSChart1.data = data
For i = 1 To MSChart1.RowCount + 1
MSChart1.Row = i + 1
n = MSChart1.data
MSChart1.Row = i
MSChart1.data = n
Next i
MSChart1.Column = 1
MSChart1.Row = MSChart1.RowCount
End Sub
Private Sub Timer1_Timer()
Shape2.BackColor = vbRed
Shape2.BackStyle = 1
Timer2.Enabled = True
Timer1.Enabled = False
End Sub
Private Sub Timer2_Timer()
Shape2.BackColor = vbBlue
Shape2.BackStyle = 1
Timer2.Enabled = True
Timer1.Enabled = False
End Sub