2014/03/26

根據視窗寬度,動態調整google adsense廣告大小

原本最笨的方法, 就是透過display:none
但實在太遜而且違反google建議, 所以跳過

所以我就想自己修改javascript, 根據screen width 之類
這個網址有列出一些可用的變數, 列在下方:

function GetWidth()  {          var x = 0;          if (self.innerHeight)          {                  x = self.innerWidth;          }          else if (document.documentElement && document.documentElement.clientHeight)          {                  x = document.documentElement.clientWidth;          }          else if (document.body)          {                  x = document.body.clientWidth;          }          return x;  }  function GetHeight()  {          var y = 0;          if (self.innerHeight)          {                  y = self.innerHeight;          }          else if (document.documentElement && document.documentElement.clientHeight)          {                  y = document.documentElement.clientHeight;          }          else if (document.body)          {                  y = document.body.clientHeight;          }          return y;  }

但改了改都不優, 隨興看看google說明,
原來其實google已經寫了基本範例, 可以參考
https://support.google.com/adsense/answer/1354736?hl=zh-Hant

我一開始誤會了, 以為一個ID只能出現一個大小
實際上是一個ID可以根據你餵給它的width與height去動態變化
我的程式碼如下:

<style type="text/css">.adslot_1 { width: 234px; height: 60px; }  /* 半橫幅*/@media (min-width:768px) { .adslot_1 { width: 468px; height: 60px; } } /* 中橫幅*/@media (min-width:960px) { .adslot_1 { width: 728px; height: 90px; } } /* 大橫幅*/</style><script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script><ins class="adsbygoogle adslot_1" style="display:inline-block;" data-ad-client="YOUR_AD_CLIENT_ID" data-ad-slot="YOUR_AD_SLOT_ID"></ins><script>(adsbygoogle = window.adsbygoogle || []).push({});</script>

2014/03/18

基於STM32 加入LCM 16*2 display (HD44780)

最近弄一個產品, 處理器是ARM STM32f10,
其中計畫加入一個簡單的顯示螢幕, 可以讓使用者知道目前系統的資訊
於是找了一個16行*2列的螢幕,
裡面使用到的是HD44780的chip吧, 大概長這樣

這個網站有基本的介紹與sample code (C語言)
** http://www.eeherald.com/section/design-guide/sample_lcd_c_programs.html

這個網站也有程式可看, 但不幸的是他寫的似乎是給8051那類的IC使用
但好處是可以將這些sample code互相比較, 找出同異
** http://www.microcontroller-project.com/alphanumeric-keypad-with-16×2-lcd.html

推薦!! 這個網址的說明是最清楚的
程式碼也是寫最棒的(for 4-bits), 還有影片分享
但有個小小錯誤需要改, 自己改吧~
** http://myactivities-mazen.blogspot.tw/2013/09/controlling-216-lcd-with-stm32f4.html

這篇寫得很詳細, 每個步驟都說明
可以study一下~
** http://web.alfredstate.edu/weimandn/lcd/lcd_initialization/lcd_initialization_index.html

以下分享從Mazen得到,
修改成 for 8-bits的版本
Thanks Mazen!

lcd_hd44780.c

#include "stm32f10x.h"#include "usb_box.h"#include "lcd_hd44780.h"#include "stm32f10x_gpio.h"#include #include #include //#include "stm32f4_discovery.h" GPIO_InitTypeDef GPIO_InitStructure;//-----------------------------------------------------------------------------void lcd_writenibble(unsigned char nibbleToWrite){  GPIO_WriteBit(LCD_GPIO, LCD_EN, Bit_SET);  GPIO_WriteBit(LCD_GPIO, LCD_D0,(BitAction)(nibbleToWrite & 0x001));  GPIO_WriteBit(LCD_GPIO, LCD_D1,(BitAction)(nibbleToWrite & 0x002));  GPIO_WriteBit(LCD_GPIO, LCD_D2,(BitAction)(nibbleToWrite & 0x004));  GPIO_WriteBit(LCD_GPIO, LCD_D3,(BitAction)(nibbleToWrite & 0x008));    GPIO_WriteBit(LCD_GPIO, LCD_D4,(BitAction)(nibbleToWrite & 0x010));  GPIO_WriteBit(LCD_GPIO, LCD_D5,(BitAction)(nibbleToWrite & 0x020));  GPIO_WriteBit(LCD_GPIO, LCD_D6,(BitAction)(nibbleToWrite & 0x040));  GPIO_WriteBit(LCD_GPIO, LCD_D7,(BitAction)(nibbleToWrite & 0x080));  udelay(200);  GPIO_WriteBit(LCD_GPIO, LCD_EN, Bit_RESET);}  //-----------------------------------------------------------------------------unsigned char LCD_ReadNibble(void){  unsigned char tmp = 0;  GPIO_WriteBit(LCD_GPIO, LCD_EN, Bit_SET);  tmp |= (GPIO_ReadInputDataBit(LCD_GPIO, LCD_D0) << 0);  tmp |= (GPIO_ReadInputDataBit(LCD_GPIO, LCD_D1) << 1);  tmp |= (GPIO_ReadInputDataBit(LCD_GPIO, LCD_D2) << 2);  tmp |= (GPIO_ReadInputDataBit(LCD_GPIO, LCD_D3) << 3);  tmp |= (GPIO_ReadInputDataBit(LCD_GPIO, LCD_D4) << 4);  tmp |= (GPIO_ReadInputDataBit(LCD_GPIO, LCD_D5) << 5);  tmp |= (GPIO_ReadInputDataBit(LCD_GPIO, LCD_D6) << 6);  tmp |= (GPIO_ReadInputDataBit(LCD_GPIO, LCD_D7) << 7);    GPIO_WriteBit(LCD_GPIO, LCD_EN, Bit_RESET);  return tmp;}  //-----------------------------------------------------------------------------unsigned char LCD_ReadStatus(void){ unsigned char status = 0; GPIO_InitStructure.GPIO_Pin   =  LCD_D0 | LCD_D1 | LCD_D2 | LCD_D3 |LCD_D4 | LCD_D5 | LCD_D6 | LCD_D7; GPIO_InitStructure.GPIO_Mode  =  GPIO_Mode_AIN; GPIO_Init(LCD_GPIO, &GPIO_InitStructure); GPIO_WriteBit(LCD_GPIO, LCD_RW, Bit_SET); GPIO_WriteBit(LCD_GPIO, LCD_RS, Bit_RESET); status = LCD_ReadNibble(); GPIO_InitStructure.GPIO_Pin   =  LCD_D0 | LCD_D1 | LCD_D2 | LCD_D3 |LCD_D4 | LCD_D5 | LCD_D6 | LCD_D7; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(LCD_GPIO, &GPIO_InitStructure); return status;} //-----------------------------------------------------------------------------unsigned char LCD_ReadOutNibble(void){  unsigned char tmp = 0;  GPIO_WriteBit(LCD_GPIO, LCD_EN, Bit_SET);  tmp |= (GPIO_ReadOutputDataBit(LCD_GPIO, LCD_D0) << 0);  tmp |= (GPIO_ReadOutputDataBit(LCD_GPIO, LCD_D1) << 1);  tmp |= (GPIO_ReadOutputDataBit(LCD_GPIO, LCD_D2) << 2);  tmp |= (GPIO_ReadOutputDataBit(LCD_GPIO, LCD_D3) << 3);  tmp |= (GPIO_ReadOutputDataBit(LCD_GPIO, LCD_D4) << 4);  tmp |= (GPIO_ReadOutputDataBit(LCD_GPIO, LCD_D5) << 5);  tmp |= (GPIO_ReadOutputDataBit(LCD_GPIO, LCD_D6) << 6);  tmp |= (GPIO_ReadOutputDataBit(LCD_GPIO, LCD_D7) << 7);    GPIO_WriteBit(LCD_GPIO, LCD_EN, Bit_RESET);  return tmp;}//-----------------------------------------------------------------------------unsigned char LCD_ReadOutStatus(void){  unsigned char status = 0;    GPIO_WriteBit(LCD_GPIO, LCD_RW, Bit_SET);    GPIO_WriteBit(LCD_GPIO, LCD_RS, Bit_RESET); status = LCD_ReadOutNibble();  return status;} //------------------------------------------------------------------------------// Function Name : lcd_writedata// Description : Send Data Select routine(RS=1 & RW=0)//------------------------------------------------------------------------------void lcd_writedata(unsigned char dataToWrite){  GPIO_WriteBit(LCD_GPIO, LCD_RW, Bit_RESET);  GPIO_WriteBit(LCD_GPIO, LCD_RS, Bit_SET);  lcd_writenibble(dataToWrite);  //LCD_ReadOutStatus();  while(LCD_ReadStatus() & 0x80);}  //-----------------------------------------------------------------------------void lcd_writecommand(unsigned char commandToWrite){  GPIO_WriteBit(LCD_GPIO, LCD_RW | LCD_RS, Bit_RESET);  lcd_writenibble(commandToWrite);  //LCD_ReadOutStatus();  while(LCD_ReadStatus() & 0x80);}  //-----------------------------------------------------------------------------void lcd_str(unsigned char * text){  while(*text)    lcd_writedata(*text++);}  //-----------------------------------------------------------------------------void lcd_locate(unsigned char x, unsigned char y){  lcd_writecommand(HD44780_DDRAM_SET | (x + (0x40 * y)));}  //-----------------------------------------------------------------------------void lcd_strxy(unsigned char * text, unsigned char x, unsigned char y){  lcd_locate(x,y);  while(*text)    lcd_writedata(*text++);}  //-----------------------------------------------------------------------------void lcd_writebinary(unsigned int var, unsigned char bitCount){  signed char i;   for(i = (bitCount - 1); i >= 0; i--)     {     lcd_writedata((var & (1 << i))?'1':'0');     }}  //-----------------------------------------------------------------------------void LCD_ShiftLeft(void){  lcd_writecommand(HD44780_DISPLAY_CURSOR_SHIFT | HD44780_SHIFT_LEFT | HD44780_SHIFT_DISPLAY);}  //-----------------------------------------------------------------------------void LCD_ShiftRight(void){  lcd_writecommand(HD44780_DISPLAY_CURSOR_SHIFT | HD44780_SHIFT_RIGHT | HD44780_SHIFT_DISPLAY);}  //-----------------------------------------------------------------------------void lcd_init(void){ volatile unsigned char i = 0; volatile unsigned int delayCnt = 0; RCC_APB2PeriphClockCmd(LCD_CLK_LINE, ENABLE); GPIO_InitStructure.GPIO_Pin = LCD_D0|LCD_D1|LCD_D2|LCD_D3|LCD_D4|LCD_D5|LCD_D6|LCD_D7|LCD_RS|LCD_RW|LCD_EN; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(LCD_GPIO, &GPIO_InitStructure); GPIO_WriteBit(LCD_GPIO, LCD_RW | LCD_RS | LCD_EN, Bit_SET); udelay(200); GPIO_WriteBit(LCD_GPIO, LCD_RW | LCD_RS | LCD_EN, Bit_RESET); udelay(200); /* Initial by instruction */ lcd_writecommand(0x030); udelay(500); lcd_writecommand(0x030); udelay(200); lcd_writecommand(0x030); udelay(200); lcd_writecommand(HD44780_FUNCTION_SET |                HD44780_FONT5x7 |                HD44780_TWO_LINE |                HD44780_8_BIT); //0x38 udelay(200); lcd_writecommand(HD44780_DISPLAY_ONOFF |                HD44780_DISPLAY_OFF); //0x08 udelay(200); lcd_writecommand(HD44780_CLEAR); //0x01 udelay(200); lcd_writecommand(HD44780_ENTRY_MODE |                HD44780_EM_SHIFT_CURSOR |                HD44780_EM_INCREMENT);//0x06 udelay(200); lcd_writecommand(HD44780_DISPLAY_ONOFF |                HD44780_DISPLAY_ON |                HD44780_CURSOR_ON |                HD44780_CURSOR_NOBLINK);//0x0E udelay(200);}  //-----------------------------------------------------------------------------void lcd_addchar (unsigned char chrNum, unsigned char n, const unsigned char *p){        lcd_writecommand(HD44780_CGRAM_SET | chrNum * 8);        n *= 8;        do         lcd_writedata(*p++);        while (--n);}  //-----------------------------------------------------------------------------void lcd_cls(void){        lcd_writecommand(HD44780_CLEAR);}   unsigned char* intToStr(int n){ int i = 0; int j = 0; char *tmp = (char*)malloc(sizeof(char));    unsigned char *ret = (unsigned char*)malloc(12); if(n<0 || n>9){  *tmp = n%10+48;  n-=n%10;  n/=10;  tmp++;  i++; }  *tmp = n+48; i++; while(i--){   ret[j++] = *tmp--; } return ret;}void lcd_int(int a){  unsigned short ch,first; //you need this to display 0 if there was any char     //Get first char    ch = a/10000;    if(ch){     lcd_writedata(48+ch);     first = 1;    }     //Get second char    ch = (a/1000)%10;    if(ch || first){     lcd_writedata(48+ch);     first = 1;    }     //Get third char    ch = (a/100)%10;    if(ch || first){     lcd_writedata(48+ch);     first = 1;    }     //Get fourth char    ch = (a/10)%10;    if(ch || first){     lcd_writedata(48+ch);     //first = 1;                   //q    }     //Get fifth char    ch = a%10;    //if(ch || first)   //you dont need to check las one if ch is 0 then just display it, unless you dont want to then uncomment this line ("//q" line too)     lcd_writedata(48+ch);       // lcd_str(intToStr(n));} void lcd_intxy(int n, unsigned char x, unsigned char y){        lcd_locate(x,y);        lcd_int(n);}

lcd_hd44780.h

//******************************************************************************//    THE SOFTWARE INCLUDED IN THIS FILE IS FOR GUIDANCE ONLY.//    AUTHOR SHALL NOT BE HELD LIABLE FOR ANY DIRECT, INDIRECT//    OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING//    FROM USE OF THIS SOFTWARE.//****************************************************************************** //##############################################################################// lcd_hd44780.h//############################################################################## //****************************************************************************////#include "main.h"  #define LCD_GPIO GPIOB#define LCD_CLK_LINE RCC_APB2Periph_GPIOB #define LCD_D0 GPIO_Pin_0#define LCD_D1 GPIO_Pin_1#define LCD_D2 GPIO_Pin_5#define LCD_D3 GPIO_Pin_8#define LCD_D4 GPIO_Pin_9#define LCD_D5 GPIO_Pin_10#define LCD_D6 GPIO_Pin_11#define LCD_D7 GPIO_Pin_12 #define LCD_RS GPIO_Pin_13#define LCD_RW GPIO_Pin_14#define LCD_EN GPIO_Pin_15//******************************************************************************//  #define HD44780_CLEAR                                          0x01 #define HD44780_HOME                                           0x02 #define HD44780_ENTRY_MODE     0x04        #define HD44780_EM_SHIFT_CURSOR    0x00        #define HD44780_EM_SHIFT_DISPLAY   0x01        #define HD44780_EM_DECREMENT    0x00        #define HD44780_EM_INCREMENT    0x02 #define HD44780_DISPLAY_ONOFF    0x08        #define HD44780_DISPLAY_OFF     0x00        #define HD44780_DISPLAY_ON                  0x04        #define HD44780_CURSOR_OFF                  0x00        #define HD44780_CURSOR_ON                   0x02        #define HD44780_CURSOR_NOBLINK    0x00        #define HD44780_CURSOR_BLINK    0x01 #define HD44780_DISPLAY_CURSOR_SHIFT 0x10        #define HD44780_SHIFT_CURSOR               0        #define HD44780_SHIFT_DISPLAY              8        #define HD44780_SHIFT_LEFT                         0        #define HD44780_SHIFT_RIGHT                  4 #define HD44780_FUNCTION_SET    0x20        #define HD44780_FONT5x7      0x00        #define HD44780_FONT5x10     0x04        #define HD44780_ONE_LINE     0x00        #define HD44780_TWO_LINE     0x08        #define HD44780_4_BIT      0x00        #define HD44780_8_BIT      0x10 #define HD44780_CGRAM_SET                                    0x40 #define HD44780_DDRAM_SET                                    0x80 //##############################################################void lcd_init(void);void lcd_cls(void);void lcd_str(unsigned char * text);void lcd_strxy(unsigned char * text, unsigned char x, unsigned char y);void lcd_locate(unsigned char x, unsigned char y);void lcd_int(int n);void lcd_intxy(int n, unsigned char x, unsigned char y); //############################################################### void lcd_writedata(unsigned char dataToWrite);void lcd_writecommand(unsigned char commandToWrite);void lcd_writebinary(unsigned int var, unsigned char bitCount);void lcd_addchar (unsigned char chrNum, unsigned char n, const unsigned char *p);

main.c

lcd_init();         while (1)          {                   lcd_locate(1,0);                  lcd_str("Mazen");                  lcd_locate(7,0);                  lcd_str("A.");                  lcd_locate(0,1);                  lcd_str("-3125");           }

STM32 透過ADC_GetFlagStatus 知道 EOC狀態並讀值

其實網路隨意google一下就很多資料

uint16_t readADC1(void) {  uint16_t value;  //ADC_RegularChannelConfig(ADC1, channel, 1, ADC_SampleTime_55Cycles5); // Start the conversion  ADC_SoftwareStartConvCmd(ADC1, ENABLE);   // Wait until conversion completion  while(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET);  ADC_ClearFlag(ADC1, ADC_FLAG_EOC); // Get the conversion value  value = ADC_GetConversionValue(ADC1);  ADC_SoftwareStartConvCmd(ADC1, DISABLE); return value;}
void AdcPro(void){ static uint16_t buf[SAMP_COUNT]; static uint8_t write; uint32_t sum; uint8_t i; //buf[write] = ADC_GetConversionValue(ADC1); 原本直接讀取資料   buf[write] = readADC1();  if (++write >= SAMP_COUNT) {  write = 0; } sum = 0; for (i = 0; i < SAMP_COUNT; i++) {  sum += buf[i]; } g_usAdcValue = sum / SAMP_COUNT; /* 取平均 */ //ADC_SoftwareStartConvCmd(ADC1, ENABLE); /* 原本先為下次使用而enable */}

如何在部落格加入code 區域 (修改css)

可以在各種部落格裡面看到許多將程式碼加入文章裡

其實只要幾個步驟就可以完成了

也就是修改css, 將下面這一段加入CSS就好啦

pre { padding: 10px 5px 10px 22px; margin: 10px 15px; border: 1px solid #CCC; font:13px/120% 'Courier New'; letter-spacing: 0.07em; background: #F5F5F5 url(https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh6r-beClagoumej1FnptO1ZkMGkgzGr5q86AlJhe4OQ15lEdwYy6lCIVyJbohqe4-QzLfwEeLJMJDbqKQX1UtZ3WGn1Gb3vGVOQfZruUU-kKdvWT4LQX8pjRPIFS_6TpAOHpYYqZlFAmg/s1600/code.gif) no-repeat scroll left top;  text-align: justify; overflow: auto; color:#333;}

完成後, 之後發表文章時
請記得在你要發布的程式碼加入 pre tag 如下:

<pre> 程式碼 </pre>

Google blogger 怎麼設定最新回應(留言)

最近搬家過來blogger 但是找不到簡單widget獲得最新留言 稍微做了一下功課, 可以透過RSS的方式去做

1. 看看網址 http://YOUR_BLOG_ADDRESS/feeds/comments/default
這個網址可以看到你部落格的最新回應RSS
像我的話, 就是 http://ezmm.blogspot.tw/feeds/comments/default

2.將此網址複製
http://YOUR_BLOG_ADDRESS/feeds/comments/default

3.加入資訊

選”資訊提供”, 再進去貼網址即可

—-