⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.c

📁 利用psoc进行usb及capsense的程序编写
💻 C
字号:
//----------------------------------------------------------------------------
// C main line for example 2
//
// Copyright 2005, John Hyde,  USB Design By Example
// You may use this program for development but you may not sell or publish it
// without the written permission of the author
//
// Send questions to john@USB_By_Example.com
//----------------------------------------------------------------------------

#include <m8c.h>        // part specific constants and macros
#include "PSoCAPI.h"    // PSoC API definitions for all User Modules
#include "hardware.h"
#include "adcinc.h"
#include "pga.h"
#include "lcd.h"
#include "usb.h"

extern BYTE SOF_Flag;
BYTE Value, Even, Buttons, StringIndex, TypingDelay;
BYTE H, T, U;
BYTE Voltage[2];
BYTE Report[8];
BYTE String[16];
BYTE const FixedString[] = {" tempx = "};
BYTE const HidUsageTable[] = {
// ASCII to HID_Usage lookup table, this table does not allow for shift
	0x2c,0x1e,0x34,0x20,0x21,0x22,0x24,0x34,0x26,0x27,0x25,0x2e,0x36,0x2d,0x37,0x38,
	0x27,0x1e,0x1f,0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x33,0x33,0x36,0x2e,0x37,0x38,
	0x1f,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,0x10,0x11,0x12,
	0x13,0x14,0x15,0x16,0x17,0x18,0x19,0x1a,0x1b,0x1c,0x1d,0x2f,0x31,0x30,0x23,0x2d,
	0x34,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,0x10,0x11,0x12,
	0x13,0x14,0x15,0x16,0x17,0x18,0x19,0x1a,0x1b,0x1c,0x1d,0x2f,0x31,0x30,0x35,0
	};
	

void init_display(void) {
	BYTE i;
	LCD_Position(0,0);
	LCD_PrCString("Temperature1=");
	LCD_Position(1,0);
	LCD_PrCString("Temperature2=");
// String will be initialized to all 0, set the non-zero values
	for (i=0; i<sizeof(FixedString); i++) String[i] = FixedString[i];
	String[14] = 0x0a;	// CR
	StringIndex = 15;	// Point to 0 at end of String
	}	

BYTE HidUsage(BYTE ASCII) {
// Convert the ASCII character into a HID usage
// This routine implements printable characters but not Shift
	if (ASCII == 0x0a) return 0x28;		// CR
	if ((ASCII > 0x7F) | (ASCII < 0x20)) return 0;
	return HidUsageTable[ASCII - 0x20];
	}
	
void ConvertToDecimal(BYTE Value) {
	H = Value/100;
	T = (Value - (H*100))/10;
	U = (Value - (H*100) - (T*10));
	}

void DisplayTemperature(BYTE Row) {
	ConvertToDecimal(Voltage[Row]);
	LCD_Position(Row, 13);
	LCD_WriteData(H + '0');
	LCD_WriteData(T + '0');
	LCD_WriteData(U + '0');
	}

void StartTyping (BYTE Button) {
	BYTE Index;
	Index = ~Button & 1;
	String[5] = Index + '1';
	ConvertToDecimal(Voltage[Index]);
	String[9] = H + '0';
	String[11] = T + '0';
	String[13] = U + '0';
	StringIndex = 0;		// This will cause typing to start
	}
	
BYTE Typing (void) {
// Called every 1 msec, if not typing then return false
	if (StringIndex == 15) return FALSE;
// Else send the next character if ready
	if (TypingDelay++ > 20) {
		TypingDelay = 0;
		Report[2] = HidUsage(String[++StringIndex]);
		USB_LoadInEP(2, &Report[0], 8, USB_TOGGLE);
		}
	return TRUE;
	}
	 
void main() {
	init_hardware();
	M8C_EnableGInt;

// Can now start my ADC
// Note that I had to include a PGA to connect the analog mux to ADC
	PGA_Start(3);
	ADCINC_Start(3);
	ADCINC_GetSamples(1);
	LCD_Start();
	init_display();
// Connect to the host
	USB_Start(0,USB_5V_OPERATION);

// Wait to be enumerated
	while (!USB_bGetConfiguration());
	USB_INT_REG |= USB_INT_SOF_MASK;
	ADCINC_GetSamples(1);
	
	while(1) {
		if (SOF_Flag) {
			SOF_Flag = 0;
			Even ^= 1;
			Buttons = scan_buttons();
			if (ADCINC_fIsDataAvailable()) {
				Voltage[Even] = ADCINC_bClearFlagGetData();
				DisplayTemperature(Even);
				MUX_CR3 = MUX_CR3 ^ 0xC0;
				ADCINC_GetSamples(1);
				}
			if (!Typing() && Buttons) StartTyping(Buttons);
			}
		}
	}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -