📄 main._c
字号:
//ICC-AVR application builder : 2007-5-24 20:31:27
// Target : M16
// Crystal: 8.0000Mhz
#include <iom16v.h>
#include <macros.h>
#include "font.h"
#define LCD_DC_PIN 0x04 // PB2
#define LCD_CE_PIN 0x02 // PB1
#define SPI_MOSI_PIN 0x20 // PB5
#define LCD_RST_PIN 0x01 // PB0
#define SPI_CLK_PIN 0x80 // PB7
#define X_Length 45
#define X_Init 10
#define Y_Init 2
/*--------------------------------------------------------------------------------------------------
Public function prototypes
--------------------------------------------------------------------------------------------------*/
void LcdInit ( void );
void LcdClear ( void );
static void LcdSend ( unsigned char, unsigned char);
static void Delay ( void );
void LcdGotoXY ( unsigned char, unsigned char );
unsigned int timer_count=0;
void port_init(void)
{
PORTA = 0x00;
DDRA = 0x00;
PORTB = 0x00;
DDRB = 0x00;
PORTC = 0x00; //m103 output only
DDRC = 0x00;
PORTD = 0x00;
DDRD = 0x00;
}
//TIMER0 initialize - prescale:1
// WGM: Normal
// desired value: 32uSec
// actual value: 32.000uSec (0.0%)
void timer0_init(void)
{
TCCR0 = 0x00; //stop
TCNT0 = 0x00; //set count
OCR0 = 0xFE; //set compare
TCCR0 = 0x01; //start timer
}
#pragma interrupt_handler timer0_comp_isr:20
void timer0_comp_isr(void)
{
//compare occured TCNT0=OCR0
TCNT0 = 0x00;
timer_count++;
}
//call this routine to initialize all peripherals
void init_devices(void)
{
//stop errant interrupts until set up
CLI(); //disable all interrupts
port_init();
MCUCR = 0x00;
GICR = 0x00;
TIMSK = 0x02; //timer interrupt sources
SEI(); //re-enable interrupts
//all peripherals are now initialized
}
void delay(void) //延时
{
unsigned int i=0;
unsigned char j=0;
for(i=1;i!=0;i++)
{
for(j=1;j!=0;j++)
;
}
}
void Delay100us(unsigned char x)
{
unsigned char i; //4clock
for(i=147;x!=0;x--)
while(--i); //5 * i clock
}
void Delay1ms( unsigned int n)
{
for (;n!=0;n--){
Delay100us(10);
}
}
void Delay1s( unsigned int m) // m <= 6 ,when m==7, it is 1.
{
m=m*40;
for (;m!=0;m--){
Delay100us(250);
}
}
/*--------------------------------------------------------------------------------------------------
Global Variables
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,10个
--------------------------------------------------------------------------------------------------*/
//字模 "FUCK JAPAN"
unsigned char const X_TAB[] =
{0x7E,0x0A,0x0A,0x0A,0x00,0x7E,0x40,0x40,0x7E,0x00,0x7E,0x42,0x42,0x42,0x00,0x7E,0x18,0x24,0x42,0x00,0x00,0x00,0x42,0x7E,0x02,0x00,0x7C,0x12,0x12,0x7C,
0x00,0x7E,0x0A,0x0A,0x0E,0x00,0x7C,0x12,0x12,0x7C,0x00,0x7E,0x08,0x10,0x7E};
////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////
/*--------------------------------------------------------------------------------------------------
Name : LcdInit
Description : Performs MCU SPI & LCD controller initialization.
Argument(s) : None.
Return value : None.
--------------------------------------------------------------------------------------------------*/
void LcdInit( void )
{
//unsigned char Clear;
// Pull-up on reset pin,上电复位端口,此时为高,不复位;
PORTB |= LCD_RST_PIN;
// Set output bits on port B,设置输出口;
DDRB |= LCD_RST_PIN | LCD_DC_PIN | LCD_CE_PIN | SPI_MOSI_PIN | SPI_CLK_PIN;
Delay();
// Toggle display reset pin.
PORTB &= ~LCD_RST_PIN; //上电复位;
Delay();
PORTB |= LCD_RST_PIN; //复位完;
// SPI初始化;Enable SPI port: No interrupt, MSBit first, Master mode, CPOL->0, CPHA->0, Clk/4
SPCR = BIT(SPE)|BIT(MSTR); //使能SPI,主机模式;
//SPSR = 0x00; //似乎不用这几句也能正常运行;
//Clear= SPSR;
//Clear= SPDR;
// Disable LCD controller
PORTB |= LCD_CE_PIN;
LcdSend( 0x21, 0 ); // LCD Extended Commands.
LcdSend( 0xC8, 0 ); // Set LCD Vop (Contrast).
LcdSend( 0x06, 0 ); // Set Temp coefficent.
LcdSend( 0x13, 0 ); // LCD bias mode 1:48.
LcdSend( 0x20, 0 ); // LCD Standard Commands, Horizontal addressing mode.
LcdSend( 0x0C, 0 ); // LCD in normal mode.
LcdClear();
}
/*--------------------------------------------------------------------------------------------------
Name : LcdClear
Description : Clears the display.
Argument(s) : None.
Return value : None.
--------------------------------------------------------------------------------------------------*/
void LcdClear(void)
{
unsigned int i;
LcdSend(0x0c, 0);
LcdSend(0x80, 0); //此时默认操作为清屏
for (i=0; i<504; i++)
LcdSend(0, 1);
}
/*--------------------------------------------------------------------------------------------------
Name : LcdGotoXY 设置块坐标
Description : Sets cursor location to xy location corresponding to basic font size.
Argument(s) : x, y -> Coordinate for new cursor position. Range: 1,1 .. 14,6
Return value : None.
--------------------------------------------------------------------------------------------------*/
void LcdGotoXY(unsigned char X, unsigned char Y)
{
LcdSend(0x40 | Y, 0); // column
LcdSend(0x80 | X, 0); // row
}
/*--------------------------------------------------------------------------------------------------
Name : LcdSend
Description : Sends data to display controller.
Argument(s) : data -> Data to be sent
cd -> Command or data (see/use enum)
Return value : None.
--------------------------------------------------------------------------------------------------*/
static void LcdSend (unsigned char data, unsigned char command)
{
PORTB &= ~LCD_CE_PIN ; // 使能LCD
if (command == 0)
PORTB &= ~LCD_DC_PIN ; // 传送命令
else
PORTB |= LCD_DC_PIN ; // 传送数据
SPDR = data; // 传送数据到SPI寄存器
while ((SPSR & 0x80) == 0); // 等待数据传送完毕
PORTB |= LCD_CE_PIN ; // 关闭LCD
}
/*--------------------------------------------------------------------------------------------------
Name : Delay
Description : Uncalibrated delay for LCD init routine.
Argument(s) : None.
Return value : None.
--------------------------------------------------------------------------------------------------*/
static void Delay ( void )
{
int i;
for ( i = -12000; i < 12000; i++ );
}
void lcd_show(unsigned int r,unsigned int DOT)
{
unsigned char tmp=0,i=0;
if(r >1000)
{
tmp=r/1000;
for(i=0;i<5;i++)
LcdSend(FontLookup[tmp][i],1);
}
if(r >100)
{
tmp=(r%1000)/100;
for(i=0;i<5;i++)
LcdSend(FontLookup[tmp][i],1);
}
if(r >10)
{
tmp=(r%100)/10;
for(i=0;i<5;i++)
LcdSend(FontLookup[tmp][i],1);
}
tmp=r%10;
for(i=0;i<5;i++)
LcdSend(FontLookup[tmp][i],1);
// 小数点
for(i=0;i<5;i++)
LcdSend(FontLookup[10][i],1);
//
for(i=0;i<5;i++)
LcdSend(FontLookup[DOT][i],1);
//K
for(i=0;i<5;i++)
LcdSend(FontLookup[11][i],1);
}
/*--------------------------------------------------------------------------------------------------
Name : main
Description :
Argument(s) :
Return value : None.
--------------------------------------------------------------------------------------------------*/
/*
void main(void)
{ unsigned char X; //v=0,按行显示,x对应某个bank(0~83)
LcdInit();
while(1)
{
LcdGotoXY(X_Init,Y_Init);
for(X=0;X<X_Length;X++)LcdSend(X_TAB[X],1);
}
}
*/
void main(void)
{
unsigned int time1=0,time2=0;
float RF=46.0,R=0.0;
unsigned int R_TMP=0,DOT=0;
timer_count=0;
init_devices(); //全部输入,内部上拉
DDRA |=(1<<2);
PORTA |=(1<<2); //PA2 输出,完成充电
Delay1ms(50); //延时等待充满
_NOP();
DDRA &=~(1<<2); //输入,
PORTA &=~(1<<2);
timer0_init();
DDRA |=(1<<1); //
PORTA &=~(1<<1); //PA1 输出低,完成放电
while(PINA&(1<<2)); //等待,放电完成,PINA2被拉低
TCCR0 = 0x00; //stop timer
time1=timer_count; //记录标准电阻的放电时间
timer_count=0;
init_devices(); //全部输入,内部上拉
DDRA |=(1<<2);
PORTA |=(1<<2); //PA2 输出,完成充电
Delay1ms(50); //延时等待充满
_NOP();
DDRA &=~(1<<2); //输入
PORTA &=~(1<<2); //清内部上拉
timer0_init();
DDRA |=(1<<0); //
PORTA &=~(1<<0); //PA0 输出低,完成放电
while(PINA&(1<<2)); //等待,让电完成,PINA2被拉低
TCCR0 = 0x00; //stop timer
time2=timer_count; //计算待测电阻的时间
//开始计算阻值
R=RF*time2/time1;
//R=1234;
LcdInit();
LcdGotoXY(X_Init,Y_Init);
R_TMP=R;
DOT=(R-R_TMP)*10;
lcd_show(R_TMP,DOT);
while(1)
;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -