/*
 * DC_DC_Buck Demo Application
 * Copyright (c) 2013-2014 China JiLiang University
 * All Rights Reserved.
 */

/*!
 * @file        main.c
 * @version     1.0
 * @author      Kai Zhang
 * @date        2013-07-22
 * @brief       DC_DC_Buck_Tiva 2PWMźţ Ƶ500Hz
 * PWM1			PC4  ֵͨPWMռձ ռձȵڷΧ 5 ~ 95 %
 * PWM2			PA6  ֵͨPWMռձ ռձȵڷΧ 5 ~ 95 %
 * PB0			ʹPC4ռձ
 * PE4			ʹPA6ռձ
 * ADC		PB5
 */

#ifndef TARGET_IS_BLIZZARD_RB1
#define TARGET_IS_BLIZZARD_RB1
#endif

#ifndef  PART_TM4C123GH6PM
#define  PART_TM4C123GH6PM
#endif

#include <stdbool.h>
#include <stdint.h>
#include "driverlib/rom.h"
#include "driverlib/adc.h"
#include "driverlib/sysctl.h"
#include "driverlib/pwm.h"
#include "driverlib/gpio.h"
#include "driverlib/pin_map.h"
#include "inc/hw_ints.h"
#include "inc/hw_memmap.h"
#include "LCDDriver.h"
#include "inc/hw_gpio.h"
#include "driverlib/interrupt.h"
/************************************************************************
 * ʼADCȡӦֵ
 * 		|
 *				|
 *		M4	 PB5|<--ADC1current		ģתźԴ
 *		________|
 *
 ***********************************************************************/

#define ADC_BASE			ADC0_BASE			// ʹADC0
#define Current_Sequence 	1					// ʹ2

void Init_ADC()

{
	ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_ADC0);					//ʹADC

	ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);				//ʹGPIOB

	ROM_GPIOPinTypeADC(GPIO_PORTB_BASE, GPIO_PIN_5);				//PB5ΪAD

	ROM_ADCSequenceConfigure(ADC_BASE, Current_Sequence, ADC_TRIGGER_PROCESSOR, 0);	//ADCã1䴥

	ROM_ADCSequenceStepConfigure(ADC_BASE, Current_Sequence, 0, ADC_CTL_CH11 | ADC_CTL_IE |	//Stepã1Step0
								 ADC_CTL_END);												//ʹͨ11(PB5<-->CH11)Ҳһжź

	ROM_ADCSequenceEnable(ADC_BASE, Current_Sequence);				//ʹܲ

	ROM_ADCIntClear(ADC_BASE, Current_Sequence);					//жϱ־
}

/************************************************************************
 *  @berif	ʼPWMȡ鷴ź
 *  @param	none
 *  @return none
 * 		|
 *				|
 * 		M4	 PC4|-->M0PWM6(SYS_PWM1)	---------Channel 1
 * 		M4	 PA6|-->M1PWM2(EN_2)		---------Channel 2
 *		________|
 *
 ***********************************************************************/
#define PERIOD_TIME			0x4E0 * 20

void Init_PWM()
{
	SysCtlPWMClockSet(SYSCTL_PWMDIV_1);					//PWMʱӣ1Ƶ

	SysCtlPeripheralEnable(SYSCTL_PERIPH_PWM0);			//ʹPWM0ģ
	SysCtlPeripheralEnable(SYSCTL_PERIPH_PWM1);			//ʹPWM1ģ

	SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);			//ʹPWM1ʹõGPIOA
	SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOC);			//ʹPWM0ʹõGPIOC

	GPIOPinConfigure(GPIO_PC4_M0PWM6);					//PC4ùPWM
	GPIOPinConfigure(GPIO_PA6_M1PWM2);					//PA6ùΪPWM

	GPIOPinTypePWM(GPIO_PORTC_BASE, GPIO_PIN_4);	//PC4ΪPWM0ģʹ
	GPIOPinTypePWM(GPIO_PORTA_BASE, GPIO_PIN_6);	//PA6ΪPWM1ģʽʹ

	PWMGenConfigure(PWM0_BASE, PWM_GEN_3, PWM_GEN_MODE_UP_DOWN | PWM_GEN_MODE_NO_SYNC);		//PWM0ģ飬PWM03¼²
	PWMGenConfigure(PWM1_BASE, PWM_GEN_1, PWM_GEN_MODE_UP_DOWN | PWM_GEN_MODE_NO_SYNC);		//PWM1ģ飬PWM11¼²

	PWMGenPeriodSet(PWM0_BASE, PWM_GEN_3, PERIOD_TIME);			//PC4MOPWM6PWM
	PWMGenPeriodSet(PWM1_BASE, PWM_GEN_1, PERIOD_TIME);			//PA6M1PWM1PWM

	PWMPulseWidthSet(PWM0_BASE, PWM_OUT_6, PERIOD_TIME / 4);		//PC4MOPWM6ռձ25%
	PWMPulseWidthSet(PWM1_BASE, PWM_OUT_2, PERIOD_TIME / 4);		//PA6M1PWM1ռձ25%

	PWMOutputState(PWM0_BASE, PWM_OUT_6_BIT, true);				//ʹ
	PWMOutputState(PWM1_BASE, PWM_OUT_2_BIT, true);				//ʹ

	PWMGenEnable(PWM0_BASE, PWM_GEN_3);							//PWM03Ķʱ
	PWMGenEnable(PWM1_BASE, PWM_GEN_1);							//PWM11Ķʱ

	PWMSyncTimeBase(PWM0_BASE, PWM_GEN_3);						//ʹܷģͬ
	PWMSyncTimeBase(PWM1_BASE, PWM_GEN_1);						//ʹܷģͬ

}

/************************************************************
 * @brief  	Զ˿CDажϳʼ
 * @param	none
 * @param	none
 ***********************************************************/
void Init_Int_Key() {

	// Make PD6/7 an output.
	SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOC);


	GPIODirModeSet(GPIO_PORTC_BASE, GPIO_PIN_7, GPIO_DIR_MODE_IN);

	GPIOPadConfigSet(GPIO_PORTC_BASE, GPIO_PIN_7, GPIO_STRENGTH_2MA,
				GPIO_PIN_TYPE_STD_WPU);

	GPIOIntTypeSet(GPIO_PORTC_BASE, GPIO_PIN_7, GPIO_LOW_LEVEL);

	GPIOIntEnable(GPIO_PORTC_BASE, GPIO_PIN_7);

	IntEnable(INT_GPIOC);

	IntMasterEnable();

	GPIOIntClear(GPIO_PORTC_BASE, GPIO_PIN_7);

}

void Int_GPIO_C_Handler(void) //жϴӺ
{
	unsigned long ulStatus;
	ulStatus = GPIOIntStatus(GPIO_PORTC_BASE, true); // ȡж״̬
	GPIOIntClear(GPIO_PORTC_BASE, ulStatus); // ж״̬Ҫ
	if (ulStatus & GPIO_PIN_7) // KEYж״̬Ч
	{
		SysCtlDelay(10 * (SysCtlClockGet() / 3000)); // ʱԼ10ms
		while (GPIOPinRead(GPIO_PORTC_BASE, GPIO_PIN_7) == 0x00); // ȴKEY̧
		SysCtlDelay(10 * (SysCtlClockGet()  / 3000)); // ʱԼ10msɼ

		SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);
		GPIOPinTypeGPIOOutput(GPIO_PORTB_BASE, GPIO_PIN_0);
		int32_t status = 0;
		status = GPIOPinRead(GPIO_PORTB_BASE, GPIO_PIN_0);
		if(!status)
		{
			GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_0, GPIO_PIN_0);

		}else{
			GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_0, 0);
		}

	}

	//HighSpeed_VCAģʹð
//	VCA_BUTTON_UP_DOWM = 0x01;
}


/********************************
 * 
 * *******************************/
int main(void)
{
	uint32_t pui32ADC_CurrentValue;			// ADCֵ
	uint32_t pui32ADCWhell_Value;			// ADCֵ

	unsigned long ADC_CurrentValue_Old = 0;			// һεADCֵ
	unsigned long ADCWhell_Value_Old = 0; 				// һεADCֵ

	uint32_t cur_Duty, old_Duty = 0;		 // ݹADCתֵǰʱֵ

	SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN |			//ϵͳʱ
			SYSCTL_XTAL_16MHZ);

//	LCD_Enable();						// ʹLCD
//	LCD_Init();							// ʼLCD
//	LCD_ScreenClr();						// 

	Init_Int_Key();

	//LCDĬʾֵ
	LCD_Draw_Chinese(1, 0, 0);			// 
	LCD_Draw_Chinese(3, 0, 16);			// 
	LCD_Draw_Char(':', 0, 30);			// :

	LCD_Draw_Char('C', 0, 0 * 8);
	LCD_Draw_Char('u', 0, 1 * 8);
	LCD_Draw_Char('r', 0, 2 * 8);
	LCD_Draw_Char('r', 0, 3 * 8);
	LCD_Draw_Char('e', 0, 4 * 8);
	LCD_Draw_Char('n', 0, 5 * 8);
	LCD_Draw_Char('t', 0, 6 * 8);
	LCD_Draw_Char('_', 0, 7 * 8);
	LCD_Draw_Char('I', 0, 8 * 8);
	LCD_Draw_Char('n', 0, 9 * 8);
	LCD_Draw_Char(':', 0, 10 * 8);
	LCD_Draw_Char('m', 1, 127-16);			// m
	LCD_Draw_Char('A', 1, 127-8);			// A


	LCD_Draw_Char('D', 2, 0 * 8);
	LCD_Draw_Char('u', 2, 1 * 8);
	LCD_Draw_Char('t', 2, 2 * 8);
	LCD_Draw_Char('y', 2, 3 * 8);
	LCD_Draw_Char(':', 2, 4 * 8);
	LCD_Draw_Char('%', 2, 9 * 8);

	Init_ADCWheel();						//ʼADģ
	Init_ADC();								//ʼģ

	Init_PWM();								// ʼPWM


	while(1)
	{
		//---------------------------------------------------------------------------------
		ADCProcessorTrigger(ADC_BASE, Current_Sequence);				//


		while(!ADCIntStatus(ADC_BASE, Current_Sequence, false))		//ȴ
		{
		}
		ADCIntClear(ADC_BASE, Current_Sequence);						//жϱ־
		ADCSequenceDataGet(ADC_BASE, Current_Sequence, &pui32ADC_CurrentValue);		//ȡֵ

		//۲ԭͼ:INA213ź50R14˵ĵΪCurrent/4096*3.3/50/0.1*1000mA
		//=(current*3.3*1000)/(50*0.1)
		unsigned long Current_Sample = (pui32ADC_CurrentValue * 660) / 4096;
		unsigned char number = '0';
		int i = 0 ;
		unsigned char data[4] = {0};
		data[0] = Current_Sample / 1000;				//ֵLCDʾʽ
		data[1] = (Current_Sample / 100) % 10;
		data[2] = (Current_Sample / 10) % 10;
		data[3] = (Current_Sample / 1) % 10;

		// ADCֵ仯ʾ
		if(Current_Sample != ADC_CurrentValue_Old)
		{
			for(i = 0; i < 4; ++i)
			{
					LCD_Draw_Char(number + data[i], 1, 50 + 10 * i);
			}

			ADC_CurrentValue_Old = Current_Sample;
		}

		//-----------------------------------------------------------------------------------------------
		ADCWheel_ValueGet(&pui32ADCWhell_Value);							//ȡADCWheelֵ
		unsigned long ADCWheel_Sample = pui32ADCWhell_Value;
		data[0] = ADCWheel_Sample / 1000;
		data[1] = (ADCWheel_Sample / 100) % 10;
		data[2] = (ADCWheel_Sample / 10) % 10;
		data[3] = (ADCWheel_Sample / 1) % 10;

		// ADCֵ仯ʾ
		if(ADCWheel_Sample != ADCWhell_Value_Old)
		{
			for(i = 0; i < 4; ++i)
			{
//				LCD_Draw_Char(number + data[i], 3, 50 + 10 * i);
			}
			ADCWhell_Value_Old = ADCWheel_Sample;
		}

		cur_Duty = 5 + (90 * pui32ADCWhell_Value) / 4096;					//ݲֵռձ

		LCD_Draw_Char(number + cur_Duty / 10, 2, 6 * 8);
		LCD_Draw_Char(number + cur_Duty % 10, 2, 7 * 8);

		if((cur_Duty - old_Duty) > 1 || (old_Duty - cur_Duty) > 1)			//ռձȷͱ仯ʱPWM
		{

			uint32_t period = cur_Duty * PERIOD_TIME / 100;

			PWMGenPeriodSet(PWM0_BASE, PWM_GEN_3, PERIOD_TIME);			//PC4˿ڳʼPWMռձ
			PWMPulseWidthSet(PWM0_BASE, PWM_OUT_6, period);
			PWMSyncTimeBase(PWM0_BASE, PWM_GEN_3_BIT);

			PWMGenPeriodSet(PWM1_BASE, PWM_GEN_1, PERIOD_TIME);			//PA6˿PWMռձ
			PWMPulseWidthSet(PWM1_BASE, PWM_OUT_2, period);
			PWMSyncTimeBase(PWM1_BASE, PWM_GEN_1_BIT);

			old_Duty = cur_Duty;
		}

		int32_t status = 0;
		status = GPIOPinRead(GPIO_PORTB_BASE, GPIO_PIN_0);
		if(status)
		{
			LCD_Draw_Char('E', 3, 0 * 8);
			LCD_Draw_Char('N', 3, 1 * 8);
			LCD_Draw_Char('1', 3, 2 * 8);

			LCD_Draw_Char('O', 3, 4 * 8);
			LCD_Draw_Char('N', 3, 5 * 8);
			LCD_Draw_Char(' ', 3, 6 * 8);
		}else{
			LCD_Draw_Char('E', 3, 0 * 8);
			LCD_Draw_Char('N', 3, 1 * 8);
			LCD_Draw_Char('1', 3, 2 * 8);
			LCD_Draw_Char('O', 3, 4 * 8);
			LCD_Draw_Char('F', 3, 5 * 8);
			LCD_Draw_Char('F', 3, 6 * 8);
		}

		ROM_SysCtlDelay(SysCtlClockGet() / 3);				//ʱ1s
	}
}
//---------------------------------------------------------------------------
