void setup_LCD() { uint8_t i; // wait 50ms for LCD to come up Delay(50); // reset pins to all 0 send_LCD(0); DelayUS(4500); for(i = 0; i < 3; i++) { // try three times to set LCD into a known state (re-sync half-byte stream) // start by setting 8 bit mode send_halfbyte(LCD_FUNCTIONSET|LCD_8BITMODE); DelayUS(4500); // wait a minimum of 4.1ms } // set in 4 bit mode send_halfbyte(LCD_FUNCTIONSET|LCD_4BITMODE); // set # of lines, font size, etc send_byte(LCD_FUNCTIONSET|LCD_2LINE|LCD_4BITMODE|LCD_5x8DOTS, 0); // TODO: does this need a modifier? // turn on display send_byte(LCD_BACKLIGHT|LCD_DISPLAYON|LCD_CURSOROFF|LCD_BLINKOFF, 0); LCD_clear(); // left to right send_byte(LCD_ENTRYMODESET|LCD_ENTRYLEFT|LCD_ENTRYSHIFTDECREMENT, 0); LCD_home(); LCD_print_char('B'); }
//涉及到的地址是Flash的绝对地址 BYTE int FlashWriteWord(U32 Addr, U16 Data) { int i, j; for(j=0; j<FLASH_SECTOR_WRITE_TRY_TIMES; j++) { FlashBaseAddr[0x555]=0xAA; FlashBaseAddr[0x2AA]=0x55; FlashBaseAddr[0x555]=0xA0; *((U16*)Addr)=Data; DelayUS(FLASH_SECTOR_WRITE_WAIT_TIME); i=FLASH_SECTOR_WRITE_LOOP_TIMES; while(i--) { if(*((U16*)Addr)==Data) { return 1; } DelayUS(FLASH_SECTOR_WRITE_LOOP_WAIT_TIME); } //Error so it should be reset before try it again FlashBaseAddr[0]=0xF0; DelayUS(FLASH_RESET_WAIT_TIME); } return 0; }
//涉及到的地址是Flash的绝对地址 BYTE int FlashSectorErase(U32 Addr) { int i, j; for(j=0; j<FLASH_SECTOR_ERASE_TRY_TIMES; j++){ FlashBaseAddr[0x555]=0xAA; FlashBaseAddr[0x2AA]=0x55; FlashBaseAddr[0x555]=0x80; FlashBaseAddr[0x555]=0xAA; FlashBaseAddr[0x2AA]=0x55; *((U16*)Addr)=0x30; DelayUS(FLASH_SECTOR_ERASE_WAIT_TIME); i = FLASH_SECTOR_ERASE_LOOP_TIMES; while(i--) { if(*((U16*)Addr)==0xFFFF) if(*((U16*)Addr+0x10)==0xFFFF) //16 WORD OFFSET if(*((U16*)Addr+0x100)==0xFFFF) //256 WORD OFFSET if(*((U16*)Addr+0x200)==0xFFFF) //512 WORD OFFSET return 1; DelayUS(FLASH_SECTOR_ERASE_LOOP_WAIT_TIME); } //Error so it should be reset before try it again FlashBaseAddr[0]=0xF0; DelayUS(FLASH_RESET_WAIT_TIME); } return 0; }
void readFromParameter(uchar paramAddr, uchar command){ // set command to COMMAND and the device write destiny to PARAM_RD uchar destAddr[2]; destAddr[0] = 0x80|paramAddr; I2C_w(SLAVE_ADDR, COMMAND, destAddr, 1); DelayUS(10); // read inform from PARAM_RD I2C_r(SLAVE_ADDR, PARAM_RD, &command, 1); DelayUS(10); }
void writeToParameter(uchar paramAddr, uchar command){ uchar destAddr[2]; destAddr[0] = 0xA0|paramAddr; // write command to PARAM_WR I2C_w(SLAVE_ADDR, PARAM_WR, &command, 1); DelayUS(10); // set command to COMMAND and the device write PARAM_WR to destiny I2C_w(SLAVE_ADDR, COMMAND, destAddr, 1); DelayUS(10); }
static int8_t send_halfbyte(uint8_t data) { if(send_LCD(data | LCD_SEND_En) < 0) { return -1; } DelayUS(1); // must be >450ns if(send_LCD(data & ~LCD_SEND_En) < 0) { return -1; } DelayUS(37); // commands need >37us to settle return 0; }
/******************************************************************************* * Function Name : Read_Y * Description : ¶ÁÈ¡ADS7843ͨµÀY+µÄADCÖµ * Input : None * Output : None * Return : ADS7843·µ»ØͨµÀY+µÄADCÖµ * Attention : None *******************************************************************************/ int Read_Y(void) { int i; TP_CS(0); DelayUS(1); WR_CMD(CHY); DelayUS(1); i=RD_AD(); TP_CS(1); return i; }
/****************************************************** * 函数名:Read_Y * 描述 :通过SPI方式读取 2046所获得的触摸 通道Y+ AD转换值 * 输入 : 无 * 输出 :Y+通道AD转换值 * 举例 :无 * 注意 :无 *********************************************************/ int Read_Y(void) { int i; CS_2046(0); DelayUS(1); WR_CMD(CHY); DelayUS(1); i=Read_touch_ad(); CS_2046(1); return i; }
/******************************************************************************* * Function Name : Read_X * Description : 读取ADS7843通道X+的ADC值 * Input : None * Output : None * Return : ADS7843返回通道X+的ADC值 * Attention : None *******************************************************************************/ int Read_X(void) { int i; TP_CS_LOW(); DelayUS(1); WR_CMD(CHX); DelayUS(1); i=RD_AD(); TP_CS_HIGH(); return i; }
/******************************************************************************* * Function Name : Read_X * Description : 读取ADS7843通道X+的ADC值 * Input : None * Output : None * Return : ADS7843返回通道X+的ADC值 * Attention : None *******************************************************************************/ int Read_X(void) { int i; TP_CS(0); //CS作用有待考虑。。。。写指令读数据时候需将CS拉低? DelayUS(1); WR_CMD(CHX); /* 通道Y+的选择控制字 */ DelayUS(1); i=RD_AD(); TP_CS(1); return i; }
//DEVICE CODE int GetFlashID(void) { int i; FlashBaseAddr[0x555]=0xAA; FlashBaseAddr[0x2AA]=0x55; FlashBaseAddr[0x555]=0x90; DelayUS(FLASH_DEVICECODE_WAIT_TIME); i = FlashBaseAddr[1]; FlashBaseAddr[0]=0xF0; DelayUS(FLASH_RESET_WAIT_TIME); return i; }
/******************************************************************************* * Function Name : Read_X * Description : Read ADS7843 ADC value of X + channel * Input : None * Output : None * Return : return X + channel ADC value * Attention : None *******************************************************************************/ static int Read_X(void) { TP_CS(0); DelayUS(1); WR_CMD(CHX); DelayUS(1); int i = RD_AD(); TP_CS(1); return i; }
/******************************************************************************* * Function Name : RD_AD * Description : 读取ADC值 ,12位精度AD??? * Input : None * Output : None * Return : ADS7843返回二字节数据 * Attention : None *******************************************************************************/ static int RD_AD(void) //static。。静态函数,保持RD的值? { unsigned short buf,temp; //short一般占用两个字节 /* Wait for SPI2 Tx buffer empty */ while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET); /* Send SPI2 data */ SPI_I2S_SendData(SPI2,0x0000); /* Wait for SPI2 data reception */ while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_RXNE) == RESET); /* Read SPI2 received data */ temp=SPI_I2S_ReceiveData(SPI2); buf=temp<<8; DelayUS(1); while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET); /* Send SPI2 data */ SPI_I2S_SendData(SPI2,0x0000); /* Wait for SPI2 data reception */ while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_RXNE) == RESET); /* Read SPI2 received data */ temp=SPI_I2S_ReceiveData(SPI2); buf |= temp; buf>>=3; //舍弃低三位,只取[14:3位],12位精度, buf&=0xfff; return buf; }
void DelayMS(uint iMs){ uint i,j; for(i=0;i<iMs;i++) for(j=0;j<3;j++){ DelayUS(1); } }
//because the minmize interval on linux is 10ms, use it when need //it maybe not exatctly void CalibrationTimeBaseValue(void) { int i, j, k=0; struct timeval tv; struct timezone tz; unsigned int s_msec, e_msec; int DelayTimes=200*1000; gettimeofday(&tv, &tz); s_msec = tv.tv_sec*1000*1000 + tv.tv_usec; for (i=0; i<DelayTimes; i++) for(j=0; j<BASELOOPTESTCNT; j++) k++; gettimeofday(&tv, &tz); e_msec = tv.tv_sec*1000*1000 + tv.tv_usec; DBPRINTF("DELAY US=%d BASECNT=%f\n", e_msec-s_msec, ((float)DelayTimes/(e_msec-s_msec))*BASELOOPTESTCNT); BASELOOPTESTCNT=(int)(((float)DelayTimes/(e_msec-s_msec))*BASELOOPTESTCNT+1); #if 0 gettimeofday(&tv, &tz); s_msec = tv.tv_sec*1000*1000 + tv.tv_usec; DelayUS(1000*1000); gettimeofday(&tv, &tz); e_msec = tv.tv_sec*1000*1000 + tv.tv_usec; DBPRINTF("DELAY US=%d\n", e_msec-s_msec); #endif }
/******************************************************************************* * Function Name : RD_AD * Description : ¶ÁÈ¡ADCÖµ * Input : None * Output : None * Return : ADS7843·µ»Ø¶þ×Ö½ÚÊý¾Ý * Attention : None *******************************************************************************/ static int RD_AD(void) { unsigned short buf,temp; /* Wait for SPI2 Tx buffer empty */ while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET); /* Send SPI2 data */ SPI_I2S_SendData(SPI2,0x0000); /* Wait for SPI2 data reception */ while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_RXNE) == RESET); /* Read SPI2 received data */ temp=SPI_I2S_ReceiveData(SPI2); buf=temp<<8; DelayUS(1); while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET); /* Send SPI2 data */ SPI_I2S_SendData(SPI2,0x0000); /* Wait for SPI2 data reception */ while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_RXNE) == RESET); /* Read SPI2 received data */ temp=SPI_I2S_ReceiveData(SPI2); buf |= temp; buf>>=3; buf&=0xfff; return buf; }
void GPIO_RS485_Status(U32 SendMode) { if (SendMode) __gpio_set_pin(RS485_SEND); /* output high level */ else __gpio_clear_pin(RS485_SEND); /* output low level */ DelayUS(1000); }
/****************************************************** * 函数名:Touch_GetAdXY * 描述 :SPI方式 读取2046 通道X+ 通道Y+的ADC值 * 输入 : 无 * 输出 :通道X+ 通道Y+的ADC值 * 举例 :无 * 注意 :无 *********************************************************/ void Touch_GetAdXY(int *x,int *y) { int adx,ady; adx=Read_X(); DelayUS(1); ady=Read_Y(); *x=adx; *y=ady; }
void TP_GetAdXY(u32 *x, u32 *y) { int adx,ady; adx=Read_X(); DelayUS(1); ady=Read_Y(); *x=(u32)adx; *y=(u32)ady; }
/******************************************************************************* * Function Name : TP_GetAdXY * Description : Read ADS7843 ADC value of X + Y + channel * Input : None * Output : None * Return : return X + Y + channel ADC value * Attention : None *******************************************************************************/ static void TP_GetAdXY(int *x, int *y) { int adx = Read_X(); DelayUS(1); int ady = Read_Y(); *x = adx; *y = ady; }
BOOL ExCheckGPI(BYTE GPIOPIN) { int i=200,c=0; while(--i) { if(!GPIOGetLevel(GPIOPIN)) if(++c>20) return FALSE; DelayUS(5); } return TRUE; }
void RS_SendDataPack(serial_driver_t *rs, unsigned char *buf, unsigned short size) { unsigned short cs; TMyBuf *packet=bget(1); PRSHeader ch=(PRSHeader)(packet->buf); memset(packet->buf, 0, sizeof(TRSHeader)); packet->len=sizeof(TRSHeader)+size; ch->HeaderTag[0]=THEDID; ch->HeaderTag[1]=THEDID; ch->Cmd=(char)CMD_RS; ch->CmdCheck=0xFF-CMD_RS; ch->Len=size; memcpy(packet->buf+sizeof(TRSHeader), buf, size); ch->CheckSum=in_chksum(packet->buf, packet->len); #ifdef MAIN_PRG if(rs==&st232) { int dd=gOptions.RS232BaudRate/10000; RS485_setmode(TRUE); if(dd>0) dd=4*115200/dd; else dd=4*115200; cs=0; while(cs<packet->len) { rs->write(packet->buf[cs++]); if(cs%256==0) { DelayUS(dd); } } rs->flush_output(); // DelayUS(3000); //for 115200 DelayUS(3000*115200/gOptions.RS232BaudRate); RS485_setmode(FALSE); } else #endif { for(cs=0;cs<packet->len;cs++) rs->write(packet->buf[cs]); rs->flush_output(); } packet->len=0; }
/* ********************************************************************************************************* * 函 数 名: XPT2046_WriteCMD * 功能说明: 写命令 * 形 参:CHX 0x90 //通道Y+的选择控制字 CHY 0xd0 //通道X+的选择控制字 * 返 回 值: 无 ********************************************************************************************************* */ static void XPT2046_WriteCMD(unsigned char cmd) { unsigned char buf; unsigned char i; // TP_CS(1); TP_DIN(0); TP_DCLK(0); // TP_CS(0); for(i=0;i<8;i++) { buf=(cmd>>(7-i))&0x1; TP_DIN(buf); //Delayus(5); DelayUS(5); TP_DCLK(1); //Delayus(5); DelayUS(5); TP_DCLK(0); } }
/* * 读取TP x y 的AD值(12bit,最大是4096) */ void Touch_GetAdXY(int *x,int *y) { int adx,ady; //adx=Read_X(); adx = XPT2046_ReadAdc(CHX); DelayUS(1); //ady=Read_Y(); ady = XPT2046_ReadAdc(CHY); *x=adx; *y=ady; }
void LCD_clear() { uint8_t x,y; // clear anything that might be on it send_byte(LCD_CLEARDISPLAY, 0); current_lcd_x = current_lcd_y = printing_lcd_x = printing_lcd_y = 0; for(x = 0; x < LCD_MAX_X; x++) { for(y = 0; y < LCD_MAX_Y; y++) { current_lcd_state[x][y] = ' '; } } DelayUS(1500); // clearing the display takes awhile }
/************************************************************************************************* * * 函数名称: * * 函数功能: 读18B20的一个字节 * * 入口参数: * * 出口参数: 无 * * 说明: * *************************************************************************************************/ uint8_t DS18B20ReadByte (void) { uint8_t i; uint8_t u; for (u = 0, i = 0; i < 8; i++) { _D_DIS_INT(); //// DQ_OUT(0); DelayUS(10); // <= 15 u >>= 1; DQ_OUT(1); //////////////////// DelayUS(5); if (DQ_IN()) { u |= 0x80; } _D_ENA_INT(); //// DelayUS(_DEBUG_R_TMPTR_TM_DLY); /////40 DQ_OUT(1); DelayUS(4); } return(u); }
/******************************************************************************* * Function Name : RD_AD * Description : 读取ADC值 * Input : None * Output : None * Return : ADS7843返回二字节数据 * Attention : None *******************************************************************************/ static int RD_AD(void) { unsigned short buf,temp; temp = WR_CMD(0x00); buf = temp<<8; DelayUS(1); temp = WR_CMD(0x00);; buf |= temp; buf>>=3; buf&=0xfff; return buf; }
/************************************************************************************************* * * 函数名称: * * 函数功能: 向18B20写入一个字节 * * 入口参数: * * 出口参数: 无 * * 说明: * *************************************************************************************************/ void DS18B20WriteByte (uint8_t wr) { uint8_t i; for (i = 0; i < 8; i++) { _D_DIS_INT();//// DQ_OUT(0); DelayUS(10); // <= 15 DQ_OUT(wr & 0x01); _D_ENA_INT();//// DelayUS(20); //延时 >15 us(写周期不少于15us) DQ_OUT(1); wr >>= 1; DelayUS(4); //延时 2 us } DelayUS(2); }
void RS_SendDataPack(serial_driver_t *rs, unsigned char *buf, int len) { U32 etime=GetTickCount(); DBPRINTF("485 send:%d\n",len); //// 485收发控制 低电平收,高电平发 RS485_setmode(TRUE); DelayUS(1000);// DEBUGTRACE(buf,len); // rs->write(0x88);//多发送一个字节,修正485网络干扰 rs->write_buf(buf,len); rs->tcdrain(); while(rs->get_lsr()!=TIOCSER_TEMT); //wait for the transmit shift register empty DBPRINTF("get_lsr() time:%d\n",GetTickCount()-etime); // mmsleep(3*115200/gOptions.RS232BaudRate); RS485_setmode(FALSE); }
/******************************************************************************* * Function Name : TouchPanel_Calibrate * Description : 校准触摸屏 * Input : None * Output : None * Return : None * Attention : None *******************************************************************************/ void TouchPanel_Calibrate(void) { uint8_t i; Coordinate * Ptr; for(i=0;i<3;i++) { LCD_Clear(White); GUI_Text(10,10,"Touch crosshair to calibrate",Black,White); DelayUS(1000 * 500); DrawCross(DisplaySample[i].x,DisplaySample[i].y); do { Ptr = Read_Ads7846(); } while( Ptr == (void*)0 ); ScreenSample[i].x = Ptr->x; ScreenSample[i].y = Ptr->y; } setCalibrationMatrix( &DisplaySample[0],&ScreenSample[0],&matrix ) ; /* 送入值得到参数 */ LCD_Clear(White); }