📄 oledwinch.c
字号:
//**********************************************************************************
// File: Mywinch.c for PPS66JC Depth Measure System
// Written By: YE ZHAOBO, R&D Center SPT.Inc. 2005.06
// Last Version 2005
//**********************************************************************************
// This code implements a PPS66JC surface interface box using the P18F6620 MCU;
// The code was compiled using the MC18 compliler ver. 8.01pl3;
// The device frequency should be 3.6864 MHz;
// The PCB is PPS66JC
//
//关键更新信息
//...存贮功能
//...已经修正时间错误
//...realtime计时
//...用户自定义时钟
//...模式控制
//...采样控制
//...支持OLED显示
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// The following files should be included in the MPLAB project:
//
// mywinch.c -- Main source code file
// winch.h
//
// Use Bootloader tech
// Support Upgrade Online
// Support E-mail: yezhaobo@hotmail.com
// All Right Reserved
//---------------------------------------------------------------------
#include <p18f6620.h> // Register definitions
#include "winch.h"
///-------------
void high_routine (void);
void low_routine (void);
void InitPorts();
void ADCone(void);
void Sample(void);
void Version(int c1,int c2);
void Dectime(void);
void SampleInit(void);
void ADNow(void);
void NoSaveSample(void);
void Newlcd_init(void);
//--Wait
void OLED_CLR(char x);
unsigned char MEM_status(void);
char rdyADC(void);
void OLED_Dis2(char c,int x,int y,int z);
void OLED_Dis1(char c,int x,int y,int z);
//--
//---------------------------------------------------------------------
// Interrupt routine 中断处理程序
//---------------------------------------------------------------------
#pragma code high_ISR = 0x08
void high_ISR(void)
{
_asm
goto high_routine
_endasm
}
#pragma code
#pragma code low_ISR=0x18
void low_ISR(void)
{
_asm
goto low_routine
_endasm
}
#pragma code
#pragma interruptlow low_routine save=PROD
void low_routine(void)
{
// INTCONbits.GIEL=1;
if(PIR1bits.RCIF!=0){
*in++=RCREG1;
if((in-Buf)>=80) in=Buf;//超过缓冲则将指针复位(指向缓冲的第一个地址)
}
}
#pragma interrupt high_routine
void high_routine (void)
{
if(INTCON3bits.INT1IF) {//down
if(PORTBbits.RB0)
depth1.u++;
else
depth1.u--;
INTCON3bits.INT1IF = 0;
}
if(INTCON3bits.INT2IF) {//up
if(PORTBbits.RB0)
depth2.u--;
else
depth2.u++;
INTCON3bits.INT2IF = 0;
}
if(INTCONbits.TMR0IF) {
TMR0H = 0x1F;
TMR0L = 0x03; //+4
d0 = d1;
// depth1.u = 100;
// depth2.u = 100;
depth.u = (depth1.u + depth2.u)/2;
// d1 = depth1.u/2 + depth2.u/2;
d1 = depth.u;
speed = d1 - d0;
// f0 = f1;
// f1 = force;
// delta = f1 - f0;
TL++;
if(TL==0x02){time.l++;TL=0;}
INTCONbits.TMR0IF = 0;
}
}
//---------------------------------------------------------------------
// ERR Functions
//---------------------------------------------------------------------
void DisERR(char x)
{
int i;
i = x*16;
OLED_Dis2(1,4,8,288);
OLED_Dis2(1,4,16,304);
OLED_Dis2(1,4,24,304);
OLED_Dis2(1,4,32,i);
}
void DisWrong(unsigned int cmdx)
{
OLED_CLR(0x00);
switch(cmdx)
{
case 1:
DisERR(1);while(1); //Com-port ERR
case 2:
DisERR(2); ErrNo=928; Delay10KTCYx(3000); NoSaveSample(); //Memory ERR
case 3:
DisERR(3); ErrNo=960; Delay10KTCYx(3000); NoSaveSample();//RealTime ERR
case 4:
DisERR(4); ErrNo=992; Delay10KTCYx(3000); NoSaveSample();//E2prom ERR
case 5:
DisERR(5); ErrNo=1024; Delay10KTCYx(3000); NoSaveSample();; // Filter ERR
default:
DisERR(0);while(1); //Unkown ERR
}
}
void WaitSSPCON2SEN(void)
{
int countx;
for(countx=0;countx<30;countx++){
if(SSPCON2bits.SEN==0) return;
}
DisWrong(3);
// do{
// if(SSPCON2bits.SEN==0) break;
// countx++;
// }while(countx<30);
}
void WaitSSPCON2PEN(void)
{
int countx;
for(countx=0;countx<30;countx++){
if(SSPCON2bits.PEN==0) return;
}
DisWrong(3);
}
void WaitSSPCON2ACKEN(void)
{
int countx;
for(countx=0;countx<30;countx++){
if(SSPCON2bits.ACKEN==0) return;
}
DisWrong(3);
}
void WaitTXSTA1TRMT(void)
{
int countx;
for(countx=0;countx<30;countx++){
if(TXSTA1bits.TRMT==1) return;
}
DisWrong(1);
}
void WaitMEM_Stu(void)
{
int countx;
char stux;
for(countx=0;countx<100;countx++){
stux = 0x40 & MEM_status();
if(!stux == 0) return;
}
MEM_CE=1;
// lcd_init();
DisWrong(2);
}
void WaitEECON1WR(void)
{
int countx;
for(countx=0;countx<200;countx++){
if(EECON1bits.WR==0) return;
}
MEM_CE=1;
// lcd_init();
DisWrong(4);
}
void WaitrdyADC(void)
{
int countx;
for(countx=0;countx<1000;countx++){
if(!rdyADC()==0) return;
}
DisWrong(5);
}
//---------------------------------------------------------------------
// Time Functions
//---------------------------------------------------------------------
void Get_RealTimeClock(void);
void Init_RealTimeClock(void);
/**********************************************************************************/
void Get_RealTimeClock(void)
{
IdleI2C();
StartI2C();
// while (SSPCON2bits.SEN); // wait until start condition is over
WaitSSPCON2SEN();
WriteI2C(0b11010000); // Write DS1307:D0H
NotAckI2C();
IdleI2C(); // ensure module is idle
if ( !SSPCON2bits.ACKSTAT ) // test for ACK condition, if received
{
WriteI2C(0x00); // DS1307 : REG Addr.
NotAckI2C();
}
IdleI2C();
StopI2C();
// while(SSPCON2bits.PEN); // wait until stop condition is over
WaitSSPCON2PEN();
//------------------------------------------------------------------------
IdleI2C();
StartI2C();
// while (SSPCON2bits.SEN); // wait until start condition is over
WaitSSPCON2SEN();
WriteI2C(0b11010001); // Read DS1307:D1H
NotAckI2C();
IdleI2C(); // ensure module is idle
if ( !SSPCON2bits.ACKSTAT ) // test for ACK condition, if received
{
real_time.Seconds = ReadI2C();
AckI2C();
// while(SSPCON2bits.ACKEN);
WaitSSPCON2ACKEN();
real_time.Minutes = ReadI2C();
AckI2C();
// while(SSPCON2bits.ACKEN);
WaitSSPCON2ACKEN();
real_time.Hours = ReadI2C();
AckI2C();
// while(SSPCON2bits.ACKEN);
WaitSSPCON2ACKEN();
real_time.Day = ReadI2C();
AckI2C();
// while(SSPCON2bits.ACKEN);
WaitSSPCON2ACKEN();
real_time.Date = ReadI2C();
AckI2C();
// while(SSPCON2bits.ACKEN);
WaitSSPCON2ACKEN();
real_time.Month = ReadI2C();
AckI2C();
// while(SSPCON2bits.ACKEN);
WaitSSPCON2ACKEN();
real_time.Year = ReadI2C();
NotAckI2C();
// while(SSPCON2bits.ACKEN);
WaitSSPCON2ACKEN();
}
IdleI2C();
StopI2C();
// while(SSPCON2bits.PEN); // wait until stop condition is over
WaitSSPCON2PEN();
}
/*------------------------------------------------------------------------------------*/
void Init_RealTimeClock(void)
{
IdleI2C();
StartI2C();
// while (SSPCON2bits.SEN); // wait until start condition is over
WaitSSPCON2SEN();
WriteI2C(0b11010000); // Write DS1307:D0H
NotAckI2C();
IdleI2C(); // ensure module is idle
if ( !SSPCON2bits.ACKSTAT ) // test for ACK condition, if received
{
WriteI2C(0x00); // DS1307 : REG Addr.
NotAckI2C();
IdleI2C();
if ( !SSPCON2bits.ACKSTAT )
{
WriteI2C(real_time.Seconds); // Configuration data
NotAckI2C();
}
IdleI2C();
if ( !SSPCON2bits.ACKSTAT )
{
WriteI2C(real_time.Minutes); // Configuration data
NotAckI2C();
}
IdleI2C();
if ( !SSPCON2bits.ACKSTAT )
{
WriteI2C(real_time.Hours); // Configuration data
NotAckI2C();
}
IdleI2C();
if ( !SSPCON2bits.ACKSTAT )
{
WriteI2C(real_time.Day); // Configuration data
NotAckI2C();
}
IdleI2C();
if ( !SSPCON2bits.ACKSTAT )
{
WriteI2C(real_time.Date); // Configuration data
NotAckI2C();
}
IdleI2C();
if ( !SSPCON2bits.ACKSTAT )
{
WriteI2C(real_time.Month); // Configuration data
NotAckI2C();
}
IdleI2C();
if ( !SSPCON2bits.ACKSTAT )
{
WriteI2C(real_time.Year); // Configuration data
NotAckI2C();
}
IdleI2C();
if ( !SSPCON2bits.ACKSTAT )
{
WriteI2C(real_time.ctr_reg); // Control REG
NotAckI2C();
}
}
IdleI2C();
StopI2C();
// while(SSPCON2bits.PEN); // wait until stop condition is over
WaitSSPCON2PEN();
}
///
void Init_RealTime(void)
{
IdleI2C();
StartI2C();
// while (SSPCON2bits.SEN); // wait until start condition is over
WaitSSPCON2SEN();
WriteI2C(0b11010000); // Write DS1307:D0H
NotAckI2C();
IdleI2C(); // ensure module is idle
if ( !SSPCON2bits.ACKSTAT ) // test for ACK condition, if received
{
WriteI2C(0x00); // DS1307 : REG Addr.
NotAckI2C();
IdleI2C();
if ( !SSPCON2bits.ACKSTAT )
{
WriteI2C(real_time.Seconds); // Configuration data
NotAckI2C();
}
IdleI2C();
if ( !SSPCON2bits.ACKSTAT )
{
WriteI2C(real_time.Minutes); // Configuration data
NotAckI2C();
}
IdleI2C();
if ( !SSPCON2bits.ACKSTAT )
{
WriteI2C(real_time.Hours); // Configuration data
NotAckI2C();
}
}
IdleI2C();
StopI2C();
// while(SSPCON2bits.PEN); // wait until stop condition is over
WaitSSPCON2PEN();
}
void Init_RealDate(void)
{
IdleI2C();
StartI2C();
// while (SSPCON2bits.SEN); // wait until start condition is over
WaitSSPCON2SEN();
WriteI2C(0b11010000); // Write DS1307:D0H
NotAckI2C();
IdleI2C(); // ensure module is idle
if ( !SSPCON2bits.ACKSTAT ) // test for ACK condition, if received
{
WriteI2C(0x04); // DS1307 : REG Addr.
NotAckI2C();
IdleI2C();
if ( !SSPCON2bits.ACKSTAT )
{
WriteI2C(real_time.Date); // Configuration data
NotAckI2C();
}
IdleI2C();
if ( !SSPCON2bits.ACKSTAT )
{
WriteI2C(real_time.Month); // Configuration data
NotAckI2C();
}
IdleI2C();
if ( !SSPCON2bits.ACKSTAT )
{
WriteI2C(real_time.Year); // Configuration data
NotAckI2C();
}
}
IdleI2C();
StopI2C();
// while(SSPCON2bits.PEN); // wait until stop condition is over
WaitSSPCON2PEN();
}
////
char hextodec(unsigned char temp)
{
unsigned char a,b,c;
a=(temp&0xf0)>>4;
b=(temp&0x0f);
c=10*a+b;
return c;
}
void Recorddepth(void)
{
IdleI2C();
StartI2C();
// while (SSPCON2bits.SEN); // wait until start condition is over
WaitSSPCON2SEN();
WriteI2C(0b11010000); // Write DS1307:D0H
NotAckI2C();
IdleI2C(); // ensure module is idle
if ( !SSPCON2bits.ACKSTAT ) // test for ACK condition, if received
{
WriteI2C(0b00010000); // DS1307 : REG Addr.0x10
NotAckI2C();
for(i=0;i<4;i++){
IdleI2C();
if ( !SSPCON2bits.ACKSTAT ){
WriteI2C(depth.c[i]); // Configuration data
NotAckI2C();
}
}
}
IdleI2C();
StopI2C();
// while(SSPCON2bits.PEN); // wait until stop condition is over
WaitSSPCON2PEN();
}
void Readrecord(void)
{
char e,i;
IdleI2C();
StartI2C();
// while (SSPCON2bits.SEN); // wait until start condition is over
WaitSSPCON2SEN();
WriteI2C(0b11010000); // Write DS1307:D0H
NotAckI2C();
IdleI2C(); // ensure module is idle
if ( !SSPCON2bits.ACKSTAT ) // test for ACK condition, if received
{
WriteI2C(0b00010000); // DS1307 : REG Addr.0x10
NotAckI2C();
}
IdleI2C();
StopI2C();
// while(SSPCON2bits.PEN); // wait until stop condition is over
WaitSSPCON2PEN();
Nop();
Nop();
///------------------------------------------------------------------------
IdleI2C();
StartI2C();
// while (SSPCON2bits.SEN); // wait until start condition is over
WaitSSPCON2SEN();
WriteI2C(0b11010001); // Read DS1307:D1H
NotAckI2C();
IdleI2C(); // ensure module is idle
if ( !SSPCON2bits.ACKSTAT ) // test for ACK condition, if received
{
for(i=0;i<4;i++){
depth1.c[i] = depth2.c[i] = ReadI2C();
// depth.c[i] = ReadI2C();
AckI2C();
// while(SSPCON2bits.ACKEN);
WaitSSPCON2ACKEN();
}
e = ReadI2C();
AckI2C();
// while(SSPCON2bits.ACKEN);
WaitSSPCON2ACKEN();
}
IdleI2C();
StopI2C();
// while(SSPCON2bits.PEN); // wait until stop condition is over
WaitSSPCON2PEN();
}
//////
void Dectime(void)
{
Get_RealTimeClock();
sec=hextodec(real_time.Seconds);
min=hextodec(real_time.Minutes);
hour=hextodec(real_time.Hours);
mday=hextodec(real_time.Date);
mon=hextodec(real_time.Month);
year=hextodec(real_time.Year)+2000;
}
unsigned int sleyear(unsigned int yr)
{
if(yr%4==0||(yr%4==0&&yr%100!=0)) return(366);
else return(365);
}
void DostoUnix(void)
{
unsigned int i;
unsigned long j;
j=0;
///从1970到现在的天数
for(i=2000; i<year; i++){
j=j+sleyear(i);///2000~2005
}
///计算本年到现在天数
for(i=0; i<(mon-1); i++){
j+=moninit[i];
}
j=j+mday;
///若本年为闰年则加1天
if(mon >= 2 && sleyear(year) == 366) j++;
j=j-1;
j=j*86400;
j=j+(hour*3600);
j=j+(min*60);
j=j+sec;
totalsec=j;
time.l=totalsec; ///给计时赋初值
Nop();
Nop();
}
///
//-------------------------------------
//-------------串口接收与发送
//-------------------------------------
char getch(void)
{
char c;
while(in==out);//相等表示没有新数据进来
c = *out;
out++;
if((out-Buf)>=80) out=Buf;
return c;
}
void SCISend(char c)
{
// while(TXSTA1bits.TRMT==0);
WaitTXSTA1TRMT();
TXREG1=c;
}
void putch(char c)
{
// while(TXSTA1bits.TRMT==0);
WaitTXSTA1TRMT();
TXREG1=c;
}
void ResetMem(void)
{
//MEM
MEM_CE=0;
MEM_CLE=0;
MEM_WE=1;
MEM_ALE=0;
MEM_RE=1;
}
//======================
//-----存储器代码-------
//======================
void SCILoadAddr(void)
{
MEM_sector = (Buf[5] & 0x0f);
MEM_block = Buf[6];
MEM_page = Buf[7];
MEM_line = Buf[8];
}
//--------------
void MEM_COMD(char command)
{
TRISD=0X00;
PORTD=0X00;
MEM_CLE=0;
//command
MEM_CLE=1;
MEM_CE=0;
MEM_ALE=0;
MEM_WE=0;
PORTD=command;
MEM_WE=1;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -