📄 lab2.c
字号:
// Lab2.c
#include "fileio.h"
#include "lcd.h"
#define linelength 16
void LCDwait (void);
void Delay(BYTE milliseconds);
void convert (WORD adres, char * stringres);
BYTE convertback (char * buffer);
unsigned int toBCD (unsigned int old);
int main (void)
{
const char name[] = "datalog.txt"; // The name of the file
unsigned int counter = 0x0000;
unsigned int counterBCD = 0x0000;
char bffr[6]; // The buffer to read from and write to
FILE * myFile; // The pointer to your file
WORD result; // The result of the A/D converstion
BYTE display;
BYTE count;
OSCCON = 0x0000;
TRISA &= 0xff00;
TRISD &= 0xff3f;
AD1PCFG = 0xFFDF;
AD1CON3 = 0x0C05;
AD1CON2 = 0xE000;
AD1CHS = 0x0505;
AD1CON1 = 0x00E7;
// Setup the LCD
mLCDInit();
LCDProcessEvents();
LCDwait();
mLCDHome();
LCDProcessEvents();
LCDwait();
/********************************************************************************
* Line 1 *
* Write the function to initialize the card *
* If the function returns 0, the program should get trapped in the infinite *
* loop
********************************************************************************/
if (! )
/*******************************************************************************/
{
LATA = 0xC0;
while(1);
}
else
{
/********************************************************************************
* Line 2 *
* Fill in the arguments for the fopen function to write a file *
********************************************************************************/
= fopen ( , );
/*******************************************************************************/
if (myFile == NULL )
{
// Code was unsuccessful- infinate loop
LATA = 0xE0;
while(1);
}
else
{
while (PORTDbits.RD13)
{
AD1CON1bits.ADON = 1;
while ( ! AD1CON1bits.DONE)
{
;
}
result = ADC1BUF0;
convert (result, bffr);
/********************************************************************************
* Line 3 *
* Fill in the arguments for the fwrite function *
* Note: The source should be typecast as a void *. One five-byte word should *
* be written from the result of the convert function *
********************************************************************************/
if (fwrite ((void *) , , , ) != 5)
/*******************************************************************************/
{
// Code was unsuccessful- infinate loop
LATA = 0xF0;
while (1);
}
Delay (50);
}
while (!PORTDbits.RD13);
/********************************************************************************
* Line 4 *
* Close the open file *
********************************************************************************/
if ( != 0)
/*******************************************************************************/
{
// Error- infinate loop
LATA = 0xF1;
while (1);
}
else
{
/********************************************************************************
* Line 5 *
* Open the file in read mode *
********************************************************************************/
= fopen ( , );
/*******************************************************************************/
if (myFile == NULL)
{
LATA = 0xF3;
while (1);
}
while(1)
{
/********************************************************************************
* Line 6 *
* Read five bytes from your file into the buffer (bffr) *
* If the number of bytes read isn't five, break out of the loop *
********************************************************************************/
while (fread ( , , , ) == 5)
/*******************************************************************************/
{
mLCDHome();
LCDwait();
mLCDPutChar('A');
LCDwait();
mLCDPutChar('/');
LCDwait();
mLCDPutChar('D');
LCDwait();
mLCDPutChar(' ');
LCDwait();
mLCDPutChar('R');
LCDwait();
mLCDPutChar('e');
LCDwait();
mLCDPutChar('s');
LCDwait();
mLCDPutChar(' ');
LCDwait();
mLCDPutChar('=');
LCDwait();
mLCDPutChar(' ');
LCDwait();
mLCDPutChar(*bffr);
LCDwait();
mLCDPutChar(*(bffr+1));
LCDwait();
mLCDPutChar(*(bffr+2));
LCDwait();
mLCDPutChar(*(bffr+3));
LCDwait();
mLCDPutChar(' ');
LCDwait();
mLCDPutChar('V');
LCDwait();
mLCDPutCmd(0xC0);
LCDwait();
counterBCD = toBCD (counter);
mLCDPutChar('L');
LCDwait();
mLCDPutChar('o');
LCDwait();
mLCDPutChar('o');
LCDwait();
mLCDPutChar('p');
LCDwait();
mLCDPutChar(':');
LCDwait();
mLCDPutChar(' ');
LCDwait();
mLCDPutChar((unsigned char) ((counterBCD / 0x1000) + 0x30));
LCDwait();
counterBCD %= 0x1000;
mLCDPutChar((unsigned char) ((counterBCD / 0x100) + 0x30));
LCDwait();
counterBCD %= 0x100;
mLCDPutChar((unsigned char) ((counterBCD / 0x10) + 0x30));
LCDwait();
counterBCD %= 0x10;
mLCDPutChar((unsigned char) (counterBCD + 0x30));
LCDwait();
display = convertback (bffr);
LATA = display & 0xF3;
LATD = (display & 0x0C) ? (((display & 0x0C) == 0x0C) ? 0xC0 : ((~((display & 0x0C) << 4)) & 0xC0)) : 0x00;
Delay (50);
}
/********************************************************************************
* Line 7 *
* Write the function to rewind your file to the beginning *
********************************************************************************/
;
/*******************************************************************************/
counter++;
}
}
}
}
}
void LCDwait (void)
{
LCDProcessEvents();
while (mLCDIsBusy())
LCDProcessEvents();
}
void Delay(BYTE milliseconds)
{
BYTE ms;
DWORD count;
ms = milliseconds;
while (ms--)
{
count = ((SYSTEM_CLOCK / 28) / 1000) - 5;
while (count--)
{
;
}
}
return;
}
void convert (WORD adres, char * stringres)
{
WORD c;
BYTE ones, tenths, hund;
c = adres;
ones = (c / 310) + 0x30;
c %= 310;
tenths = (c / 31) + 0x30;
c %= 31;
hund = (c / 3) + 0x30;
if (hund == 0x3A)
{
tenths++;
hund = 0x30;
if (tenths == 0x3A)
{
tenths = 0x30;
ones++;
}
}
stringres[0] = ones;
stringres[1] = '.';
stringres[2] = tenths;
stringres[3] = hund;
stringres[4] = ',';
stringres[5] = 0x00;
return;
}
BYTE convertback (char * buffer)
{
float result = 0.00;
result += ((unsigned char)buffer[0] - 0x30);
result += (((unsigned char)buffer[2] - 0x30) * .1);
result += (((unsigned char)buffer[3] - 0x30) * .01);
if (result >= 0.00 && result <= .4125)
return 0x01;
if (result > .4125 && result <= .825)
return 0x02;
if (result > .825 && result <= 1.2375)
return 0x04;
if (result > 1.2375 && result <= 1.65)
return 0x08;
if (result > 1.65 && result <= 2.0625)
return 0x10;
if (result > 2.0625 && result <= 2.475)
return 0x20;
if (result > 2.475 && result <= 2.8875)
return 0x40;
if (result > 2.8875 && result <= 3.3)
return 0x80;
else
return 0xff;
}
unsigned int toBCD (unsigned int old)
{
unsigned int retval = 0x0000;
if (old == 0x2710)
{
old = 0;
return 0x0000;
}
retval += ((old / 1000) * 0x1000);
old %= 1000;
retval += ((old / 100) * 0x100);
old %= 100;
retval += ((old / 10) * 0x10);
old %= 10;
retval += old;
return retval;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -