?? lib_nrf6350.c
字號:
/* Copyright (c) 2008 Nordic Semiconductor. All Rights Reserved.
*
* The information contained herein is property of Nordic Semiconductor ASA.
* Terms and conditions of usage are described in detail in NORDIC
* SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
*
* Licensees are granted free, non-transferable use of the information. NO
* WARRENTY of ANY KIND is provided. This heading must NOT be removed from
* the file.
*
* $LastChangedRevision$
*/
/** @file
*
* @author Rune Brandsegg
*
*/
#include <Nordic\reg24le1.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "nordic_common.h"
#include "lib_nrf6350.h"
#define DDRAM_ADR 0x80 // Write to DDRAM AC
#define DDRAM_WR 0x40 // Write to DDRAM
#define FUNC_SET 0x00 // Enter LCD Function settings
#define LCD_ADDR 0x3E // LCD display adr
#define JS_ADDR 0x3F // Joystick adr
#define X 0 // Contents in joystick array
#define Y 1
void w2_master_init(hal_w2_clk_freq_t clock_mode);
void w2_master_rx_data(uint8_t slave_addr, uint8_t *buffer);
void w2_master_tx_data(uint8_t slave_addr, uint8_t *buffer, uint8_t length);
void delay_ms(uint16_t ms);
void delay_10us(uint8_t us);
static bool w2_init = false;
void lcd_init(void)
{
if(!w2_init)
{
w2_master_init(HAL_W2_400KHZ); // System 2-Wire to master
w2_init = true;
}
lcd_set_instruction(0x38); // Function set
lcd_set_instruction(0x39); // Choose two-line mode
lcd_set_instruction(0x14); // Internal OSC frequency
lcd_set_contrast(LCD_CONTRAST_HIGH); // Contrast set (low byte)
lcd_set_instruction(0x5F); // Power/ICON control/
// Contrast set (high byte)
lcd_set_instruction(0x6A); // Follower control
delay_ms(200);
lcd_on(); // Display ON
lcd_clear(); // Clear display
lcd_set_instruction(0x06); // Entry mode set
}
void lcd_write_string(char *text, uint8_t line, uint8_t pos)
{
char str[18];
uint8_t buffer[2];
uint8_t i;
delay_ms(10);
if(line == 0)
{
line = 0x00; // Upper row of LCD display
}
else
{
line = 0x40; // Lower row of LCD display
}
if(pos > 15) pos = 16; // Write to visible positions
buffer[0] = FUNC_SET; // Enter function setting
buffer[1] = DDRAM_ADR + (pos + line); // LCD adr counter set to pos
w2_master_tx_data(LCD_ADDR, buffer, 2) ; // Write the settings to the
// LCD display
for(i=0;i<17;i++) // Save text in a new string
{ // with space for function
str[i+1] = text[i]; // setting
}
str[0] = DDRAM_WR; // Enter function setting
w2_master_tx_data(LCD_ADDR, str, strlen(text) + 1); // Transmit string to LCD
}
void lcd_clear(void)
{
uint8_t buffer[2];
delay_ms(10);
buffer[0] = FUNC_SET;
buffer[1] = 0x01; // Clear display
w2_master_tx_data(LCD_ADDR, buffer, 2);
delay_ms(10);
}
void lcd_set_contrast(lib_nrf6350_lcd_contrast_t contrast)
{
uint8_t buffer[2];
delay_ms(10);
buffer[0] = FUNC_SET;
buffer[1] = 0x70 | contrast; // Contrast set (low byte)
w2_master_tx_data(LCD_ADDR, buffer, 2);
delay_ms(10);
}
void lcd_set_instruction(uint8_t instr)
{
uint8_t buffer[2];
delay_ms(10);
buffer[0] = FUNC_SET;
buffer[1] = instr; // Instr. set
w2_master_tx_data(LCD_ADDR, buffer, 2);
delay_ms(10);
}
void lcd_on(void)
{
uint8_t buffer[2];
delay_ms(10);
buffer[0] = FUNC_SET;
buffer[1] = 0x0C; // Display ON
w2_master_tx_data(LCD_ADDR,&buffer[0],2);
delay_ms(10);
}
void lcd_off(void)
{
uint8_t buffer[2];
delay_ms(10);
buffer[0] = FUNC_SET;
buffer[1] = 0x08; // Display OFF
w2_master_tx_data(LCD_ADDR, buffer, 2);
delay_ms(10);
}
void js_get_value(int8_t *val)
{
uint8_t js_data;
uint8_t rx_buffer[1];
if(!w2_init)
{
w2_master_init(HAL_W2_400KHZ); // System 2-Wire to master
w2_init = true;
}
delay_10us(10);
w2_master_rx_data(JS_ADDR, rx_buffer); // Get data from the joystick
js_data = (~rx_buffer[0] & 0x1D); // Select the useful bits
if((js_data & BIT_0) == BIT_0) // Check joystick position
{
val[X] = -1;
}
else if((js_data & BIT_4) == BIT_4)
{
val[X] = 1;
}
else
{
val[X] = 0;
}
if((js_data & BIT_2) == BIT_2)
{
val[Y] = 1;
}
else if((js_data & BIT_3) == BIT_3)
{
val[Y] = -1;
}
else
{
val[Y] = 0;
}
}
bool js_button_pushed(void)
{
uint8_t js_data;
uint8_t rx_buffer[1];
if(!w2_init)
{
w2_master_init(HAL_W2_400KHZ); // System 2-Wire to master
w2_init = true;
}
delay_10us(10);
w2_master_rx_data(JS_ADDR, rx_buffer); // Get data from the joystick
js_data = (~rx_buffer[0] & BIT_1); // Mask button bit
return (js_data == BIT_1); // Check if button is pushed
}
void w2_master_init(hal_w2_clk_freq_t clock_mode)
{
hal_w2_enable(true); // Enable i2c
hal_w2_set_clk_freq(clock_mode); // Set the clock frequency
hal_w2_set_op_mode(HAL_W2_MASTER); // Set system to master mode
INTEXP |= BIT_2; // Enable 2 wire interrupts
hal_w2_all_irq_enable(true); // Enable interrupts in the 2-wire
}
void w2_master_rx_data(uint8_t slave_addr, uint8_t *buffer)
{
uint8_t i = 0;
hal_w2_transmit_start_cond(); // Transmit start condition
hal_w2_write_data((slave_addr << 1)|BIT_0); // Write the adress to listen to,
// and set the direction bit to 1
while(!SPIF) // Wait for ack
;
SPIF = 0; // Clear the interrupt flag
if(hal_w2_get_status() & BIT_1) // Check the acknowledge received
{
return;
}
while(i == 0)
{
while(!SPIF) // Wait until 2-Wire irq flag is set
;
i = hal_w2_get_status(); // Store the status from the 2-Wire
SPIF = 0; // Clear the interrupt flag
if(!(i & BIT_0)) // Check dataReady bit for interrupt
{
i = 0; // Clear the stored status
}
}
hal_w2_transmit_stop_cond(); // Transmit stop condition
buffer[0] = hal_w2_read_data(); // Read received data
}
void w2_master_tx_data(uint8_t slave_addr, uint8_t *buffer, uint8_t length)
{
uint8_t i;
uint8_t j = 0;
hal_w2_transmit_start_cond(); // Transmit start condition
hal_w2_write_data(slave_addr << 1); // Write the address to transmit to
while(!SPIF) // Wait for ack
;
SPIF = 0; // Clear the interrupt flag
if(hal_w2_get_status() & BIT_1) // Check the acknowledge received
{
return;
}
for(i = 0; i < length; i++) // Go through the data array
{
hal_w2_write_data(buffer[i]); // Write data to the 2-Wire
if(i == (length - 1)) // If all data is sent
{
hal_w2_transmit_stop_cond(); // Transmit stop condition
}
while(j == 0)
{
while(!SPIF) // Wait until 2-Wire irq flag is set
;
j = hal_w2_get_status(); // Store the status from the 2-Wire
SPIF = 0; // Clear the interrupt flag
if(!(j & BIT_0)) // Check dataReady bit for interrupt
{
j = 0; // Clear the stored status
}
}
delay_10us(4); // Short hold
}
}
void delay_ms(uint16_t ms)
{
uint16_t i, cnt;
for(i = 0; i < ms; i++)
{
cnt = 1137;
while(cnt--)
;
}
}
void delay_10us(uint8_t us)
{
uint8_t i, cnt, cnt2;
for(i = 0; i < us; i++)
{
cnt = 4;
while(cnt--)
{
cnt2 = 1;
while(cnt2--)
;
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -